就像标题中所说的,邮件返回FALSE到我调用error_get_last(),并且它返回NULL。该电子邮件从未收到。问题是,该站点上另一个页面中几乎相同的代码可以毫无问题地发送HTML电子邮件,因此服务器必须可以。
我搜索发现了以下问题“ PHP mail()返回false,但未记录任何错误”,但是答案似乎不适用于我的情况。
我的php代码是:
<?php
$MSG_Info = array(
'', // 0 = Sender's name
'', // 1 = Sender's email address
'', // 2 = Subject of the message
'');// 3 = The message itself
$MSG_SendTo ='XXXX@yahoo.com';
function GetMessageText()
{
global $MSG_Info;
return
"<blockquote>".
"Name: ".$MSG_Info[0]."<br />".
"Email: ".$MSG_Info[1]."<br />".
"Subject: ".$MSG_Info[2]."<br />".
"Message: ".wordwrap($MSG_Info[3], 70, "\r\n")."<br />".
"</blockquote>";
}
function GetHTMLHeader()
{
return
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'.
'<html>'.
'<head>'.
'<meta http-equiv="Content-Type" content="text/html;charset=utf-8">'.
'<title>Marguerite Gabe Braun</title>'.
'</head>'.
'<body>'.
'<p><font size="+2" color="#0000AA">'.
'<i>A Message from Marguerite’s Web Site</font></i></p>';
}
function SendMessageEmail($Dest)
{
global $MSG_Info, $MSG_SendTo;
$from = $MSG_SendTo;
$subject = $MSG_Info[2];
$message = GetHTMLHeader();
switch ($Dest)
{
case 1:
$to = $MSG_SendTo;
$replyto = $MSG_Info[1];
$message .= "<p>".$MSG_Info[0]." has sent you a message from your web site.</p>\n";
break;
case 2:
$to = $MSG_Info[1];
$replyto = $MSG_SendTo;
$message .= "<p>Here is the message you sent to Marguerite Gabe Braun from her web site.</p>\n";
break;
default:
$to = $MSG_SendTo;
$replyto = $MSG_SendTo;
$message .= "<p>".$MSG_Info[0]." has sent you a message from your web site.</p>\n";
}
$message .= "<p>".GetMessageText()."</p>\n"."</body></html>";
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
$headers .= "From: " . $from . PHP_EOL;
$headers .= "Reply-To: " . $replyto . PHP_EOL;
// Send the email.
error_reporting(E_ALL);
$MSG_Result = mail($to, $subject, $message, $headers);
if ($MSG_Result)
{
$MSG_Error = NULL;
}
else
{
$MSG_Error = error_get_last();
}
return $MSG_Error;
}
?>
我插入了各种代码(此处未显示)以验证我没有弄乱电子邮件地址,mail()确实返回FALSE,error_get_last()返回NULL,正确的信息在POST等中传递。
谢谢。
编辑。
强力调试,一次注释掉一行,将其缩小到我指定“ From:”的标题行中。如果我将该行注释掉,则邮件将发送出去,并说它来自我网站的网站站长。
因此,我进行了更多测试,它看起来像“发件人:”一词或以此开头的任何行都从我的消息中删除了。这么奇怪。请参见下面的代码。 Reply-to行的$ message代码,其中包含三个“ froms”的行以及简单的$ from行都显示在我的电子邮件中。其他所有人都不见了。也许这更清楚。消息行2、5、6、7、8和10显示在我的消息中。其他都以“ From:”开头的其他人则没有。这是最愚蠢的事情。
<?php
$message .= "From: ".$from."\r\n";
$message .= "Reply-To: ".$replyto."\r\n";
$message .= "From: "."\r\n";
$message .= "FROM: "."\r\n";
$message .= "From, from, FROM: "."\r\n";
$message .= "Reply-To: ".$replyto."\r\n";
$message .= $from."\r\n";
$message .= "Reply-To: ".$replyto."\r\n";
$message .= "From: ".$from."\r\n";
$message .= "Reply-To: ".$replyto."\r\n";
$message .= "<p>".GetMessageText()."</p>\n"."</body></html>";
// This commented-out code is used on another page and with works correctly.
// $headers = "MIME-Version: 1.0\r\n";
// $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
// $headers .= "From: ".$from."\r\n";
// $headers .= "Reply-To: ".$replyto."\r\n";
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
// $headers .= "From: " . $from . PHP_EOL;
$headers .= "Reply-To: " . $replyto . PHP_EOL;
// Send the email.
$MSG_Result = mail($to, $subject, $message, $headers);
if ($MSG_Result)
{
$MSG_Error = NULL;
}
else
{
$MSG_Error = error_get_last();
if ($MSG_Error == NULL) {$MSG_Error[0] = "Unknown mail() error.";}
}
return $MSG_Error;
}
?>