PHP表单发送成功消息,但mail()失败,并且永远不会收到电子邮件.什么';这个PHP脚本错了


PHP form sends success message, but mail() fails and email is never received. What's wrong with this PHP script?

我已经处理了一段时间,似乎无法让PHP mail((函数正常工作。我在下面的脚本中找不到错误。这似乎很奇怪,但脚本似乎适用于特定域(@gmail.com-收到电子邮件(、(@me.com和其他-未收到电子邮件(。这就是代码——它所要做的就是填写表格并将数据发送到提供的电子邮件地址。任何见解都将不胜感激——我对PHP不是很熟悉,我不会称自己为程序员,也不会假装自己是——我是一名设计师,所以请温柔一点。这是我购买的模板。。。所以我没有写…:S

<?php
//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$message = ($_GET['message']) ?$_GET['message'] : $_POST['message'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$message) $errors[count($errors)] = 'Please enter your message.'; 
//if the errors array is empty, send the mail
if (!$errors) {
// ====== Your mail here  ====== //
$to = 'Customers Name <customer@customerswebsite.com>'; 
//sender
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Message from your website'; 
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
    <tr><td>Name:</td><td>' . $name . '</td></tr>
    <tr><td>Email:</td><td>' . $email . '</td></tr>
    <tr><td>Message:</td><td>' . nl2br($message) . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = sendmail($to, $subject, $message, $from);
//if POST was used, display the message straight away
if ($_POST) {
    if ($result) echo 'Thank you! We have received your message.';
    else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that 
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
    echo $result;   
}
//if the errors array has values
} else {}

//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "'r'n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "'r'n";
$headers .= 'From: ' . $from . "'r'n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
?>

虽然这可能是一个IT错误,但对于SMTP,您可以做一些事情——尝试使用代码通过服务器获取电子邮件。

与IT相关的注意事项:

  1. 确保服务器的ip地址解析回您发送电子邮件地址的网站的域名。如果它说类似于";localhost";在邮件头中消息可能因为不是来自合法MTA。既然你可以把它发送到gmail帐户,看看那里的标题
  2. 验证ip地址和域名是否在RBL上或真实时间块列表。共享主机可能会导致其他人处于相同状态框以阻止您的IP地址。这是RBL检查器的链接
  3. 某些域要求使用SPF或发件人配置文件框架记录

根据我的经验,做到这一点的最佳方法是研究标头,并确定您试图针对的服务器或服务及其策略。路由器在回复你的电子邮件时通常不会给你反馈。有时是由于消息的内容,有时是由于构建消息的方式(不正确的MIME类型(。

虽然大多数PHP邮件((应用程序电子邮件都会通过许多防火墙、路由器和反垃圾邮件客户端,但您可能需要单独使用套接字和SMTP来处理邮件。在编写自己的设置之前,请先查看mail()中的设置如果你不熟悉套接字和头,那么一旦你走上这条路,可能会非常耗时另一个与配置登录参数以使用真实电子邮件帐户有关的问题。通过这种方式,如果存在反弹,您可以实际接收反弹。

还可以考虑强制PHP脚本登录到真正的MTA,并使用用户名和密码进行发送。这个问题可能会有所帮助。这样,如果有人不喜欢你的网络服务器发出的电子邮件,你就不会把你的IP列入黑名单或黑名单。

以下是使用梨通过MTA发送电子邮件的另一种方法。

这实际上将归结为黑客攻击你试图攻击的服务器的实际连接,而不会被列入黑名单。