php PHPMailer 邮件程序错误:邮件正文为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13816571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPMailer Mailer Error: Message body empty
提问by user1547410
I'm trying to get the basic example working for PHPMailer.
我正在尝试获取适用于 PHPMailer 的基本示例。
All I done was upload the whole PHPMailer_5.2.2 folder, configured the page below as per the code you see and I keep getting Mailer Error: Message body empty, but I can clearly see the contents.html file has html in it and isn't empty. This is the example file I'm using from the PHPMailer PHPMailer_5.2.2/examples/test_smtp_gmail_basic.php
我所做的只是上传整个 PHPMailer_5.2.2 文件夹,根据您看到的代码配置下面的页面,我不断收到Mailer Error: Message body empty,但我可以清楚地看到 contents.html 文件中包含 html 并且不为空。这是我从 PHPMailer PHPMailer_5.2.2/examples/test_smtp_gmail_basic.php 使用的示例文件
I tried using the settings I have in Outlook for Gmail that works, I know my username and password, the SMTP port is 587 and it's set to TLS, I tried replacing SSL with TLS in the code below, I still get same error.
我尝试使用 Outlook for Gmail 中的设置有效,我知道我的用户名和密码,SMTP 端口是 587 并且设置为 TLS,我尝试在下面的代码中用 TLS 替换 SSL,但我仍然遇到相同的错误。
I also tried the following code, which has been suggested:
我还尝试了以下代码,已建议:
changed this:
改变了这一点:
$mail->MsgHTML($body);
to this
对此
$mail->body = $body;
I still got same error, is there something else I need to configure? It's my first time using PHPMailer, I can get the standard php mail working, but I want to try this because the page I'm going to be emailing has lots of html and I don't want to have to go through all the html and enter character escapes so someone recommending using this.
我仍然遇到同样的错误,还有什么我需要配置的吗?这是我第一次使用 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 = "[email protected]"; // GMAIL username
$mail->Password = "xxx"; // GMAIL password
$mail->SetFrom('[email protected]', '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!";
}
?>
回答by Eduardo
I had the same problem and I solved just changing:
我遇到了同样的问题,我解决了改变:
This:
这个:
$mail->body = 'Hello, this is my message.';
Into this:
进入这个:
$mail->Body = 'Hello, this is my message.';
bodyinto Bodywhat a little mistake!
body成Body什么小错误!
回答by emilh8
First I had to enable the ssl extension, (which I have obtained from another post on this site). To do that, open php.ini and enable php_openssl.dll by changing
首先,我必须启用 ssl 扩展(这是我从本网站的另一篇文章中获得的)。为此,打开 php.ini 并通过更改启用 php_openssl.dll
;extension=php_openssl.dll
to
到
extension=php_openssl.dll
Now, enable tls and use port 587. Then comment out the preg_replace.
现在,启用 tls 并使用端口 587。然后注释掉preg_replace.
Finally, I was able to make it show up in my gmail "Sent" file, although I was expecting to see it in my "Inbox".
最后,我能够让它显示在我的 gmail“已发送”文件中,尽管我希望在我的“收件箱”中看到它。
This site is awesome! Thanks.
这个网站太棒了!谢谢。
回答by Popnoodles
PHPMailer->MsgHTML() tries to do something clever with fixing non-absolute URLs, but for me too it just returns empty.
PHPMailer->MsgHTML() 试图通过修复非绝对 URL 来做一些聪明的事情,但对我来说它也只是返回空。
$mail->IsHTML(true);
$mail->Body=$body;

