PHP_Mailer赢得';t打印调试信息


PHP_Mailer won't print debug information

我正试图使用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

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

感谢

假设您使用的是PHPMailer的未修改版本,那么您的某些属性/方法使用的情况不正确。来自PHPMailer的简单示例,可在https://github.com/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';
}

使用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
}