This question already has answers here:
PHP mail function doesn't complete sending of e-mail
                                
                                    (26个答案)
                                
                        
                                2年前关闭。
            
                    
我正在尝试将表单输入字段中的数据提交到电子邮件地址,同时将表单数据插入数据库表中。我已经成功地设法将表单数据提交到数据库中,但现在我正努力将表单数据作为电子邮件发送。这是我下面的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>


需要补充的一点是,当我尝试使用一个文件提交以上所有代码的应用程序时,在提交时,没有任何数据连同该电子邮件中的代码发送到数据库中。如果我删除同一文件中电子邮件的代码,则申请表将提交到数据库。我必须尽可能简洁,希望有人能帮助我!谢谢!

最佳答案

按照此行:

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

语法为:


  布尔邮件(字符串$ to,字符串$ subject,字符串$ message [,字符串$ additional_headers [,字符串$ 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注入,请使用准备好的语句:


https://en.wikipedia.org/wiki/Prepared_statement


还要确保所有POST数组都是正确的并保持值。

如果您的PHP中有任何错误,错误报告将有所帮助:


http://php.net/manual/en/function.error-reporting.php


您也可能在标题之前输出。

或者删除echo并将其替换为标题,反之亦然;您不能同时使用两者。

如果您仍然在发送邮件方面遇到问题,请参考以下问答:


PHP mail form doesn't complete sending e-mail

08-08 02:22
查看更多