PHPMailer Mailer错误:消息正文空白或空白屏幕

时间:2016-06-02 20:57:13

标签: php gmail phpmailer

我试图使用PHPMailer并且收到错误消息正文为空或屏幕为空并且不打印消息已发送。我的文件夹结构是一切都在同一文件夹中进行测试,以防任何人需要知道。我也跟着这篇文章,但似乎没有用。 PHPMailer Mailer Error: Message body empty

    <html>
<head>
<title>PHPMailer - SMTP (Gmail) basic test</title>
</head>
<body>

<?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');
echo $body;
//$body             = preg_replace('/[\]/','',$body);
//$body = "To view the message, please use an HTML compatible email viewer!";

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.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       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "no-reply@yourdomain.com";  // GMAIL username
$mail->Password   = "xxx";            // GMAIL password

$mail->SetFrom('no-reply@yourdomain.com', 'First Last');

//$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->IsHTML(true);
$mail ->MsgHTML($body);
$mail->Body = $body;
//$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

//$mail->IsHTML(true);


$address = "someemail@yourdomain.com";
$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!";
}

?>

</body>
</html>

Contents.html:

<body style="margin: 10px;">
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<br>
<br>
&nbsp;This is a test of PHPMailer.<br>
<br>
This particular example uses <strong>HTML</strong>, with a &lt;div&gt; tag and inline<br>
styles.<br>
<br>
Also note the use of the PHPMailer logo above with no specific code to handle
including it.<br />
Included are two attachments:<br />
phpmailer.gif is an attachment and used inline as a graphic (above)<br />
phpmailer_mini.gif is an attachment<br />
<br />
PHPMailer:<br />
Author: Andy Prevost (codeworxtech@users.sourceforge.net)<br />
Author: Marcus Bointon (coolbru@users.sourceforge.net)<br />
</div>
</body>

我已将回声放入代码中进行故障排除,以确定是否正在获取数据。我认为这实际上与邮件托管的Gmail有关。但它的用途是什么。非常感谢您提供任何方向。

克里斯

1 个答案:

答案 0 :(得分:0)

试试这个

 <html>
<head>
<title>PHPMailer - SMTP (Gmail) basic test</title>
</head>
<body>

<?php

//error_reporting(E_ALL);

date_default_timezone_set('America/Toronto');

include "PHPMailer_5.2.4/class.phpmailer.php"; 
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$message='<body style="margin: 10px;">
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<br>
<br>
&nbsp;This is a test of PHPMailer.<br>
<br>
This particular example uses <strong>HTML</strong>, with a &lt;div&gt; tag and inline<br>
styles.<br>
<br>
Also note the use of the PHPMailer logo above with no specific code to handle
including it.<br />
Included are two attachments:<br />
phpmailer.gif is an attachment and used inline as a graphic (above)<br />
phpmailer_mini.gif is an attachment<br />
<br />
PHPMailer:<br />
Author: Andy Prevost (codeworxtech@users.sourceforge.net)<br />
Author: Marcus Bointon (coolbru@users.sourceforge.net)<br />
</div>
</body>';

$mail             = new PHPMailer();

//$body             = file_get_contents('contents.html');
echo $message;
//$body             = preg_replace('/[\]/','',$body);
//$body = "To view the message, please use an HTML compatible email viewer!";

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.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       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "no-reply@yourdomain.com";  // GMAIL username
$mail->Password   = "xxx";            // GMAIL password

$mail->SetFrom('no-reply@yourdomain.com', 'First Last');

//$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->IsHTML(true);
$mail ->MsgHTML($message);
$mail->Body = $message;
//$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

//$mail->IsHTML(true);


$address = "someemail@yourdomain.com";
$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!";
}

?>

</body>
</html>