PHP_Mailer不会打印调试信息

时间:2016-08-03 15:20:20

标签: php smtp phpmailer

我尝试使用PHP Mailer SMTP发送电子邮件,但是即使在我打开SMTPDebug之后也没有显示任何内容,我只是在$ Mail-> send()时出错,

这就是我所拥有的

$Mail = new PHPMailer();          
$Mail->SMTPDebug = 2;
$Mail->IsSMTP(); 
$Mail->SMTPAuth = true;
$Mail->Host = "smtp.gmail.com";
$Mail->SMTPSecure = "tls"; 
$Mail->Port = 587; 
$Mail->Priority = 1;
$Mail->CharSet = 'UTF-8';
$Mail->Username = 'xxxxx@yyyyyyy.com';
$Mail->Password = 'xxxxxxxxxxxxxxxxxx';
$Mail->Encoding = '8bit';
$Mail->Subject = $subject;
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From = "xxxxxxx@yyyyyyyyy.com";
$Mail->FromName = "XXXXXXXXXXXXXXXXX";
$Mail->AddReplyTo($replyToEmail, $replyToName);
$Mail->WordWrap = 998; // RFC 2822 Compliant for Max 998 characters per line
$Mail->IsHTML(true);    
$Mail->body=$tpl;
$Mail->AddAddress($email);
$Mail->Send(); <------ false and doesn't print out why

我无法显示调试信息,有人知道为什么吗?

由于

2 个答案:

答案 0 :(得分:1)

假设您使用的是未经修改的PHPMailer版本,那么您的某些属性/方法用法使用的是不正确的大小写。来自https://github.com/PHPMailer/PHPMailer提供的PHPMailer简单示例:

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

答案 1 :(得分:0)

使用try catch

try {
$Mail = new PHPMailer(true);   //<-- edit add true here        
$Mail->SMTPDebug = 2;
$Mail->IsSMTP(); 
$Mail->SMTPAuth = true;
$Mail->Host = "smtp.gmail.com";
$Mail->SMTPSecure = "tls"; 
$Mail->Port = 587; 
$Mail->Priority = 1; //Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Username = 'xxxxx@yyyyyyy.com';
$Mail->Password = 'xxxxxxxxxxxxxxxxxx';
$Mail->Encoding = '8bit';
$Mail->Subject = $subject;
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From = "xxxxxxx@yyyyyyyyy.com";
$Mail->FromName = "XXXXXXXXXXXXXXXXX";
$Mail->AddReplyTo($replyToEmail, $replyToName);
$Mail->WordWrap = 998; // RFC 2822 Compliant for Max 998 characters per line
$Mail->IsHTML(true);    
$Mail->body=$tpl;
$Mail->AddAddress($email);
$Mail->Send(); //<------ false and doesn't print out why
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Error from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Other error messages
}