【发布时间】: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);有什么需要吗?