【发布时间】:2015-03-27 02:06:57
【问题描述】:
一年前我做了一个“原始”的 PHP 联系表单发送器,经过多次测试,它工作正常。 它应该只向 Gmail 帐户发送一封电子邮件,其中包含提交的电子邮件地址或电子邮件正文中包含的联系表单参数。
现在在重新设计时,我回到了联系表格,随机测试了它,它根本没有发送任何东西。没有错误消息,提交的表单永远不会到达主 Gmail 收件箱,不像以前那样。
<?php
$errors = '';
$myemail = 'x@gmail.com';
$noreply = 'x@y.com';
if (empty($_POST['email']))
{
$errors .= "\n Error: Please provide an e-mail address where we can send the newsletter and special deals";
}
$email_address = $_POST['email'];
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: $email_address";
$email_body = "You have received a newsletter signup. ".
"Email: $email_address";
$headers = "From: $noreply\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: newsletter_submit.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
这个网站上另一个类似的表单确实发生了这种情况,我想知道代码是否有问题(虽然它曾经工作过)或 Gmail 改变了一些东西。我尝试将表单发件人电子邮件列入白名单,但没有任何改变。
【问题讨论】:
-
您的系统可能未设置为显示错误。将错误报告添加到您打开 PHP 标记之后的文件顶部,例如
<?php error_reporting(E_ALL); ini_set('display_errors', 1);,然后是其余代码,以查看它是否产生任何结果。另外,您的服务器可能已被列入黑名单。 -
另外,将
mail($to,$email_subject,$email_body,$headers);替换为if(mail($to,$email_subject,$email_body,$headers)){ echo "mail sent"; } else { echo "error, check your logs"; }- 如果您看到“邮件已发送”,PHP/服务器已经完成了它的工作。之后发生的事情将超出您的控制范围。 -
也许您应该检查您的服务器是否没有被列入黑名单发送垃圾邮件,如果您使用的是共享主机