没有从我的php联系表单收到电子邮件

时间:2015-04-25 19:20:39

标签: php forms email

我有一个contact.html表单,用于将字段提交给contact.php脚本;但是,我没有收到收件箱上的电子邮件。是的:电子邮件地址是正确的。这里是form web page的地址 这是contact.php的代码

<?php

    $my_email = "jlvazquez825@gmail.com";
    $final_msg = "";

    //validation function
    function validate($data) {
        $data = trim($data); //removing white space and tabs
        $data = stripslashes($data); //removing \
        $data = htmlspecialchars($data); //converting to html
        return $data;
    }

    if($_SERVER['REQUEST_METHOD'] == "POST") {
            $name = validate($_POST["name"]);
            $email = validate($_POST["email"]);
            $subject = validate($_POST["subject"]);
            $message = validate($_POST["message"]);

    }
    $final_msg += "name: " . $name . "\n" .
                 "email: " . $email . "\n" .
                 "subject: " . $subject . "\n" .
                 "message: ". $message;

    $headers = "From: miatech@sohotechnology.org \r\n" . "Reply-To: " . $email . "\r\n";

    if(mail($my_email, $subject, $final_msg, $headers)) {
        echo "<b>Your Message Was Successfuly Sent!... Thanks</b>" . "<br>";
        echo "<b>name: </b>" . $name . "<br>";
        echo "<b>email: </b>" . $email . "<br>";
        echo "<b>subject: </b>" . $subject . "<br>";
        echo "<b>message: </b>" . $message;
    }
    else {
        echo "<b>Your Message Could not be Sent!... Sorry</b>";
    }


?>

1 个答案:

答案 0 :(得分:1)

使用错误的连接运算符。

正确的字符串是.=,而不是+=

$final_msg .= '...';

然后,如果表单已发送,则所有内容都应处于状态。现在,您尝试在每次加载脚本时发送邮件,这取决于表单(成功)发送的事实。

if($_SERVER['REQUEST_METHOD'] == "POST") {
    // sanitize form data, prepare $headers, $final_msg here
    if (mail(...)) {
        ...
    } else {
        ...
    }
}

条件应为

if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) &&
    !empty($_POST['name']) && 
    !empty($_POST['subject']) &&
    !empty($_POST['message'])
) {
    //
} else {
    // incomplete form was sent, or data are incorrect
}

insteda if($_SERVER['REQUEST_METHOD'] == "POST") {