PHPMailer邮件发送错误:消息主体为空。

7

我正在尝试让PHPMailer的基本示例代码工作。

我所做的就是上传整个PHPMailer_5.2.2文件夹,并按照您看到的代码配置下面的页面,但我一直收到“邮件发送错误:消息正文为空”的错误提示,尽管我能清楚地看到contents.html文件中有HTML内容并且不是空的。我正在使用PHPMailer PHPMailer_5.2.2/examples/test_smtp_gmail_basic.php中的示例文件。

我尝试使用在Outlook中适用的Gmail设置,我知道我的用户名和密码,SMTP端口是587,启用了TLS,我尝试将以下代码中的SSL替换为TLS,但仍然收到相同的错误提示。

我还尝试了建议使用的以下代码:

将这个改为:

$mail->MsgHTML($body);

转换成这样

$mail->body = $body;

我仍然遇到同样的错误,我需要配置其他东西吗?这是我第一次使用PHPMailer,我可以发送标准的PHP邮件,但我想尝试这个,因为我要发送的页面有很多HTML代码,我不想每次都输入字符转义,所以有人建议我使用这个工具。

<?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = preg_replace('/[\]/','',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 587;                   // set the SMTP port for the GMAIL server
$mail->Username   = "xxx@gmail.com";  // GMAIL username
$mail->Password   = "xxx";            // GMAIL password

$mail->SetFrom('xxx@gmail.com', 'First Last');

$mail->AddReplyTo("xxx","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "xxx.net";
$mail->AddAddress($address, "John Doe");

//$mail->AddAttachment("images/phpmailer.gif");      // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

?>

你能把 var_dumpbody 变量的结果粘贴过来吗?$mail = new PHPMailer(); $body = file_get_contents('contents.html'); $body = preg_replace('/[]/','',$body); var_dump($body); $mail->IsSMTP(); // 告诉类使用 SMTP - Gerrit Bertier
当我运行那段代码时,它只会显示为空值,我还尝试了将完整的URL www.mysite.com/PHPMailer_5.2.2/examples/contents.html 放入其中。当我访问该URL时,我可以看到HTML。 - user1547410
我第一次尝试使用 $body = "this is a test",但被拒绝了。我收到了谷歌的电子邮件,称其为可疑并被阻止了,因此我进行了一些验证步骤。现在,如果正文是纯文本,我就可以发送它了。但是,一旦我恢复了以下内容:$body = file_get_contents('contents.html'); $body = preg_replace('/[]/','',$body);我仍然得到空消息,并且运行您的代码时我得到NULL。另外一件事是,即使它发送了纯文本消息,我也没有收到电子邮件。 - user1547410
奇怪的是,当我删除这行代码后它就正常工作了? $body = preg_replace('/[]/','',$body); 那个对什么有用吗? - user1547410
不会的,因为你可以控制发送的HTML代码,不应该发生替换。 - Gerrit Bertier
显示剩余4条评论
4个回答

20

我也遇到了同样的问题,我通过改变以下内容来解决它:

这个:

$mail->body = 'Hello, this is my message.';

变成了这个样子:

$mail->Body = 'Hello, this is my message.';

body 变成了 Body,只是一个小错误!


真是让我眼睛疼啊。不过还是谢谢你! - Syafiqur__

1
PHPMailer的MsgHTML()函数会尝试处理非绝对路径的URL,但对我而言,它只返回空。
$mail->IsHTML(true);
$mail->Body=$body;

0

当将$mail->Body设置为变量时,正文为空。 当我将字符串附加到正文时,一切正常。 解决方法是strval()一个包含您的HTML代码的变量。

$message;//Your HTML message code
$mail->Body = strval($message);

0
首先,我必须启用ssl扩展(这是我从本站的另一篇帖子中获得的)。为此,请打开php.ini并通过更改来启用php_openssl.dll。
;extension=php_openssl.dll

extension=php_openssl.dll

现在,启用tls并使用端口587。然后注释掉preg_replace

最后,我成功让它出现在我的gmail“已发送”文件夹中,尽管我本来期望看到它在我的“收件箱”中。

这个网站真是太棒了!谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接