PHPMailer 邮件程序错误 : Message body empty

标签 php email phpmailer

我正在尝试让基本示例适用于 PHPMailer。

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

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

我也尝试了下面的代码,有人建议:

改变了这个:

$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!";
}

?>

最佳答案

我遇到了同样的问题,只是改变一下就解决了:

这个:

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

进入这个:

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

body 变成 Body 真是个小错误!

关于PHPMailer 邮件程序错误 : Message body empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13816571/

相关文章:

php - 使用 smtp 和 dkim 通过 postfix 使用 phpmailer 发送邮件

PHP:跳到 strpos() 给出的位置

php - preg_replace 在引号后大写字母

java - PHP 命令 shell_exec() 不适用于我的自定义 Java app5

wordpress - 如何在没有邮件接收客户端的情况下验证 Mandrill 自定义发送域?

html - @font-face 只对内联字体系列不起作用

php - 如何在 phpunit 中模拟 git diff 的结果

MYSQL 更新 IF 子字符串存在于另一个表中内连接

regex - Outlook:如何过滤在正文中明确提到我名字的电子邮件

phpmailer 在美国东部时间下午 2 点之前的 12 月 4 日开始工作