use*_*410 3 php email phpmailer
我正在尝试使用PHPMailer的基本示例.
我所做的只是上传整个PHPMailer_5.2.2文件夹,按照你看到的代码配置下面的页面,我不断得到Mailer Error: Message body empty,但我可以清楚地看到contents.html文件中有html并且不是空的.这是我在PHPMailer中使用的示例文件PHPMailer_5.2.2/examples/test_smtp_gmail_basic.php
我尝试使用我在Outlook for Gmail中运行的设置,我知道我的用户名和密码,SMTP端口是587并且它设置为TLS,我尝试在下面的代码中用TLS替换SSL,我仍然得到相同的错误.
我也尝试了以下代码,有人建议:
改变了这个:
$mail->MsgHTML($body);
Run Code Online (Sandbox Code Playgroud)
对此
$mail->body = $body;
Run Code Online (Sandbox Code Playgroud)
我仍然有同样的错误,还有其他我需要配置的东西吗?这是我第一次使用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!";
}
?>
Run Code Online (Sandbox Code Playgroud)
小智 11
我有同样的问题,我解决了改变:
这个:
$mail->body = 'Hello, this is my message.';
Run Code Online (Sandbox Code Playgroud)
进入:
$mail->Body = 'Hello, this is my message.';
Run Code Online (Sandbox Code Playgroud)
body变成Body了一个小小的错误!
小智 0
首先,我必须启用 ssl 扩展(我从本网站的另一篇文章中获得了该扩展)。为此,请打开 php.ini 并通过更改启用 php_openssl.dll
;extension=php_openssl.dll
Run Code Online (Sandbox Code Playgroud)
到
extension=php_openssl.dll
Run Code Online (Sandbox Code Playgroud)
现在,启用 tls 并使用端口 587。然后注释掉preg_replace.
最后,我能够让它显示在我的 gmail“已发送”文件中,尽管我希望在我的“收件箱”中看到它。
这个网站太棒了!谢谢。