【问题标题】:PHPMailer Mailer Error: Message body emptyPHPMailer 邮件程序错误:邮件正文为空
【发布时间】:2012-11-28 18:55:20
【问题描述】:

我正在尝试获取适用于 PHPMailer 的基本示例。

我所做的只是上传整个 PHPMailer_5.2.2 文件夹,根据您看到的代码配置下面的页面,我不断收到Mailer Error: Message body empty,但我可以清楚地看到 content.html 文件中有 html 并且不是t 为空。这是我在 PHPMailer PHPMailer_5.2.2/examples/test_smtp_gmail_basic.php 中使用的示例文件

我尝试使用 Outlook for Gmail 中有效的设置,我知道我的用户名和密码,SMTP 端口是 587 并且它设置为 TLS,我尝试在下面的代码中将 SSL 替换为 TLS,我仍然得到相同的结果错误。

我也尝试了以下代码,已被建议:

改变了这个:

$mail->MsgHTML($body);

到这里

$mail->body = $body;

我仍然遇到同样的错误,还有什么需要配置的吗?这是我第一次使用 PHPMailer,我可以让标准的 php 邮件正常工作,但我想试试这个,因为我要发送电子邮件的页面有很多 html,我不想浏览所有的 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_dump 的结果粘贴到body 变量上? $mail = 新 PHPMailer(); $body = file_get_contents('contents.html'); $body = preg_replace('/[]/','',$body); var_dump($body); $mail->IsSMTP(); // 告诉类使用 SMTP
  • 当我运行该代码时它只是显示为空,我还尝试将完整的网址放入 www.mysite.com/PHPMailer_5.2.2/examples/contents.html 当我访问该网址时,我可以看到html
  • 我第一次尝试 $body = "this is a test" 它拒绝了它,我收到一封来自谷歌的电子邮件,说它可疑并被阻止,所以我通过了一些验证步骤,现在我可以发送如果正文是纯文本,但一旦我放回去: $body = file_get_contents('contents.html'); $body = preg_replace('/[]/','',$body);我仍然收到空消息并运行您的代码我得到 NULL 另一件事是即使它发送了纯文本消息我没有收到电子邮件
  • 您能否发布您的文件夹结构的屏幕截图/概览?
  • 奇怪,当我删除这条线时它工作正常吗? $body = preg_replace('/[]/','',$body);有什么需要吗?

标签: php email phpmailer


【解决方案1】:

当设置$mail-&gt;Body 等于变量时,主体为空。 当我在身体上系上一根绳子时,一切正常。 解决方案是 strval() 包含您的 HTML 代码的变量。

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

【讨论】:

    【解决方案2】:

    首先我必须启用 ssl 扩展,(我从本网站的另一篇文章中获得)。为此,打开 php.ini 并通过更改启用 php_openssl.dll

    ;extension=php_openssl.dll
    

    extension=php_openssl.dll
    

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

    最后,我能够让它显示在我的 gmail“已发送”文件中,尽管我希望在我的“收件箱”中看到它。

    这个网站太棒了!谢谢。

    【讨论】:

      【解决方案3】:

      我有同样的问题,我解决了只是改变:

      这个:

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

      进入这个:

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

      body 变成 Body 真是个小错误!

      【讨论】:

      • 该死,这伤害了我的眼睛。还是谢谢!
      【解决方案4】:

      PHPMailer->MsgHTML() 试图做一些聪明的事情来修复非绝对 URL,但对我来说它也只是返回空。

      $mail->IsHTML(true);
      $mail->Body=$body;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-26
        • 2015-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多