我最近开始使用HTML,Javascript和PHP来创建自己的网站。 我一直在尝试创建一个PHP脚本,从表单中获取输入,使用javascript和php验证,然后发送电子邮件。 我知道脚本已执行并运行正常,因为它成功地将我重定向到最后的新页面。但是,由于某种原因,它实际上并不发送任何电子邮件。 这是代码:
<?php
$errors = '';
$myemail = '<removed due to privacy>';
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
现在我确实看到了一些类似的问题,但由于我在php方面非常不称职,所以他们并没有真正帮助。 如果你能找出原因,我将非常感激。
答案 0 :(得分:1)
$sent = mail($to,$email_subject,$email_body,$headers);
提出这样的条件
if($sent){ header('Location: contact-form-thank-you.html'); } else { echo 'mail failed';}
如果显示&#34;邮件失败&#34;将localhost配置为发送邮件
按照本教程
http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/