【发布时间】:2019-06-10 16:52:43
【问题描述】:
我是 PHPMailer 的新手,当我设置代码以使用 PHPMailer 发送电子邮件时,它向我显示错误:
邮件程序错误:邮件正文为空
我尝试了许多来自堆栈的链接,例如this 和this,但找不到解决方案。
下面是我的代码
<?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->msgHTML改为$mail->body -
它告诉我 - PHP 致命错误:调用未定义的方法 PHPMailer\PHPMailer\PHPMailer::body()
-
对不起,有一个错字。
Body必须以大写 B 开头。再试一次。 -
@MNJ 首先转储此
file_get_contents('message.html')并查看其工作情况。 -
它仍然显示 - 邮件错误:邮件正文为空