为什么我的PHP脚本没有给我发电子邮件?

时间:2016-09-19 00:14:28

标签: php jquery html

我希望我的联系表单在填写完成后向我发送电子邮件,用户点击提交。到目前为止,该页面将带我到“您的消息已被发送!”页面(confirmation.htm),但我从未收到过电子邮件。我将电子邮件放在

部分
$to = "myEmail@mail.com";

我也检查了我的垃圾邮件文件夹,它也没有去那里。

这是我的联系表格(HTML):

<div class="span">

<form action="mailer.php" method="post" class="comments-form contact-form">
<input type="text" name="name" placeholder="Your Name*" class="name" />
<input type="text" name="email" class="email" placeholder="Your Email*" /> 
<select>
<option value="Subject">Subject</option>
<option value="content-writing">Content Writing</option>
<option value="other">Other</option>
</select>
<textarea class="message" type="text" name="comment" placeholder="Your Message*"></textarea>
<input name="submit" type="submit" class="submit-comment" value="Send Message" />
</form>
</div>

然后这是我的php:

<?php
if(isset($_POST['submit'])) {
$to = "myEmail@mail.com";
$subject = "Message from your Portfolio!";

// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);

//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";

// sending the message
mail($to, $subject, $body);

// redirect to confirmation
header('Location: /confirmation.htm');
} else {
echo "Something went wrong :(";
}
?>

1 个答案:

答案 0 :(得分:-1)

这不是您的问题的答案,但我想告诉您为什么您的脚本始终重定向,即使它没有发送电子邮件。

您需要检查以确保邮件实际已发送。 PHP mail()函数将返回一个布尔值(true或false),具体取决于邮件是否已发送,因此......

if(mail($to, $subject, $body)) {
    // yay! mail() returned TRUE; mail was sent!
    header('Location: /confirmation.htm');
} else {
    // boo! mail() returned FALSE; mail was not sent :(
    echo "Something went wrong :(";
}

希望这会有所帮助。 :)

相关问题