我正在尝试将提交时表单输入字段中的数据发送到电子邮件地址,同时将表单数据插入到数据库表中。我已成功设法将表单数据提交到数据库中,我现在正在努力将表单数据作为电子邮件发送。这是我的以下PHP代码。所有PHP代码都在一个文件中。我试图将数据发送到的电子邮件地址,不是gmail,hotmail等电子邮件,它是公司自己的邮寄地址。
<?php
$conn = mysqli_connect("", "", "", ""); --> deleted this because of this post
if (!$conn) {
die ("Connection failed: ". mysqli_connect_error());
}
$course = $_POST ['course'];
$firstname = $_POST ['firstname'];
$surname = $_POST ['surname'];
$DOB = $_POST ['DOB'];
$gender = $_POST ['gender'];
$address1 = $_POST ['address1'];
$address2 = $_POST ['address2'];
$city = $_POST ['city'];
$postcode = $_POST ['postcode'];
$mobile = $_POST ['mobile'];
$home = $_POST ['home'];
$email = $_POST ['email'];
$residency = $_POST ['residency'];
$learning = $_POST ['learning'];
$qualifications = $_POST ['qualifications'];
$sql = "INSERT INTO form (course, firstname, surname, DOB, gender, address1, address2, city, postcode, mobile, home, email, residency, learning, qualifications) VALUES ('$course', '$firstname', '$surname', '$DOB', '$gender', '$address1', '$address2', '$city', '$postcode', '$mobile', '$home', '$email', '$residency', '$learning', '$qualifications')";
$result = mysqli_query($conn, $sql);
以上是将表单数据导入数据库的代码。以下代码是我尝试将信息发送到电子邮件地址。
if(isset($_POST["submit"])){
// Checking For Blank Fields..
if(
$_POST["course"]==""|| $_POST["firstname"]==""||
$_POST["surname"]==""||
$_POST["DOB"]==""||
$_POST["gender"]==""||
$_POST["address1"]==""||
$_POST["address2"]==""||
$_POST["city"]==""||
$_POST["postcode"]==""||
$_POST["mobile"]==""||
$_POST["home"]==""||
$_POST["email"]==""||
$_POST["residency"]==""||
$_POST["learning"]==""||
$_POST["qualifications"]=="")
{
echo "Please ensure all fields are filled in.";
} else {
// Check if the "Sender's Email" input field is filled out
$email=$_POST['email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Email";
}
else{
$course = $_POST['course'];
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$DOB = $_POST['DOB'];
$gender = $_POST['gender'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$postcode = $_POST['postcode'];
$mobile = $_POST['mobile'];
$home = $_POST['home'];
$email = $_POST['email'];
$residency = $_POST['residency'];
$learning = $_POST['learning'];
$qualifications = $_POST['qualifications'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender
// Send Mail By PHP Mail Function
mail("example@example.com", $course, $firstname, $surname, $DOB, $gender, $address1, $address2, $city, $postcode, $mobile, $home, $email, $residency, $learning, $qualifications);
echo "Your mail has been sent successfuly! We will get back to as soon as we can!";
}
}
}
header("Location: apply.php");
以下是我hmtl中标签的第一部分:
<form class="form" id="contact_form" action="submitApplication.php" method="post">
提交按钮:
<button type="submit" name="submit" class="btn btn-primary btn-lg outline hvr-grow">Submit Application</button>
要添加的东西,当我尝试在一个文件中提交包含上述所有代码的应用程序时,在提交时,没有数据与同一文件中的电子邮件代码一起发送到数据库。如果我删除同一文件中的电子邮件代码,则将申请表提交给数据库。我必须尽可能简洁,希望有人能帮助我!谢谢!
答案 0 :(得分:0)
按照这一行:
mail("example@example.com", $course, $firstname, $surname, $DOB, $gender, $address1, $address2, $city, $postcode, $mobile, $home, $email, $residency, $learning, $qualifications);
这不是mail()
的工作方式http://php.net/manual/en/function.mail.php
语法为:
bool mail(string $ to,string $ subject,string $ message [,string $ additional_headers [,string $ additional_parameters]])
mail()
的参数太多,应该只有四个。为要发送的所有变量分配一个变量,并将其设置为“body”参数,并使用手册中的标题。
即:
$to = "example@example.com";
$subject = "Mail form submitted";
$headers = 'From:'. $email . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender
$body = "
$course \n $firstname \n $surname \n $DOB \n $gender \n
$address1 \n $address2 \n $city \n $postcode \n $mobile \n
$home \n $email \n $residency \n $learning \n $qualifications
";
if(mail($to, $subject, $body, $headers)){
echo "Mail sent";
}
else {
echo "Error, check your logs";
// header("Location: apply.php"); exit;
// use echo OR header, not both and uncomment/comment out the one you want.
}
您的代码也可以使用SQL注入,使用预准备语句:
还要确保所有POST阵列都正确并保持值。
如果您的PHP出现任何错误,错误报告将会有所帮助:
您也可能在标题之前输出。
您删除echo
并将其替换为标题,反之亦然;你不能同时使用它们。
如果您仍然无法发送邮件,请参阅以下问答: