【问题标题】:PHPMailer error "Message body empty" with GmailGmail的PHPMailer错误“邮件正文为空”
【发布时间】:2019-06-10 16:52:43
【问题描述】:

我是 PHPMailer 的新手,当我设置代码以使用 PHPMailer 发送电子邮件时,它向我显示错误:

邮件程序错误:邮件正文为空

我尝试了许多来自堆栈的链接,例如thisthis,但找不到解决方案。

下面是我的代码

<?php
   // Import PHPMailer classes into the global namespace
   // These must be at the top of your script, not inside a function
   use PHPMailer\PHPMailer\PHPMailer;
   use PHPMailer\PHPMailer\Exception;

   require 'src/Exception.php';
   require 'src/PHPMailer.php';
   require 'src/SMTP.php';

   //Create a new PHPMailer instance
   $mail = new PHPMailer;
   $mail->isSMTP();
   $mail->SMTPDebug = 2;
   $mail->Host = 'abc@test.com';
   $mail->Port = 587;
   $mail->SMTPAuth = true;
   $mail->Username = 'abc@gmail.com';
   $mail->Password = '*****';
   $mail->setFrom('xyz@gmail.com', 'test');
   $mail->addReplyTo('xyz@gmail.com', 'Test');
   $mail->addAddress('example@gmail.com', 'Receiver Name');
   $mail->Subject = 'PHPMailer SMTP message';
   $mail->msgHTML(file_get_contents('message.html'), __DIR__);
   $mail->AltBody = 'This is a plain text message body';
   /*$mail->addAttachment('test.txt');*/
   if (!$mail->send()) {
     echo 'Mailer Error: ' . $mail->ErrorInfo;
   } else {
     echo 'Message sent!';
  }
?>

我该如何解决这个问题?

【问题讨论】:

  • 尝试将$mail-&gt;msgHTML改为$mail-&gt;body
  • 它告诉我 - PHP 致命错误:调用未定义的方法 PHPMailer\PHPMailer\PHPMailer::body()
  • 对不起,有一个错字。 Body 必须以大写 B 开头。再试一次。
  • @MNJ 首先转储此file_get_contents('message.html') 并查看其工作情况。
  • 它仍然显示 - 邮件错误:邮件正文为空

标签: php phpmailer


【解决方案1】:

msgHTML() 的调用将设置普通和HTML 正文部分。如果您对file_get_contents 的调用没有返回任何内容,那么您最终会得到一个空的正文,这将触发您看到的错误。

所以先检查那个调用:

$body = file_get_contents('message.html');
var_dump($body);
$mail->msgHTML($body, __DIR__);

这仍会显示错误,但您可以先检查您的内容。如果您的名为message.html 的文件不为空,请使用绝对路径检查(例如,使用__DIR__ . '/message.html'),如果仍然无法产生结果,请检查访问该文件的用户是否有足够的权限执行此操作。

请注意,msgHTML 也设置了 AltBody,因此除非您特别想覆盖它,否则您也不需要设置它。

【讨论】:

    猜你喜欢
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 2017-08-28
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多