从PHPMailer异常获取完整的错误消息 - php

我正在使用PHPMailer类发送smtp邮件,但是我需要从mysql表中的PHPmailerException跟踪日志,

代码:

 $mail = new PHPMailer(true);
    try {
    $mail->Host =$dmmodel->host;
    $mail->Username= $dmmodel->username;
    $mail->Password= $dmmodel->password;
    $mail->Mailer='smtp';
    $mail->Port=$dmmodel->port;
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = ($dmmodel->smtp_enableSSL==1?'ssl':($dmmodel->smtp_enableSSL==2 ? 'tls':''));
    $mail->SMTPDebug = 2;
    $mail->From = $dmmodel->email_from;
    $mail->FromName = $dmmodel->name_from;
    $mail->Subject    = $this->getContentBody($docmodel->content_subject,$docmodel->crm_base_contact_id,$_POST["taskid"],$postcode,$recall_by,$recall_dt,true);
    $mail->AddAddress($email[$i]);
    $mail->IsHTML(true); 
}
   catch (phpmailerException $e)
    {
         $msg = "Email Delivery failed -" .  $e->errorMessage();
         echo "Email not sent";
    } 

其中$ msg变量只有消息“ SMTP错误:无法验证”
但是当我发出警报时,我收到此问题的完整消息(如下),

SMTP->从服务器:220 smtp.gmail.com ESMTP n3sm27565307paf.13-gsmtp
SMTP->从服务器:250-smtp.gmail.com为您服务,[122.164.189.101]
250尺寸35882577
250-8BITMIME
250-AUTH登录PLATINA XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250个增强的状态代码
250管道
250个
250 SMTPUTF8
SMTP->错误:服务器未接受密码:535-5.7.8用户名和密码未接受。了解更多
535 5.7.8 https://support.google.com/mail/?p=BadCredentials n3sm27565307paf.13-gsmtp
SMTP->从服务器:250 2.1.5刷新n3sm27565307paf.13-gsmtp
电子邮件未发送

如何将以上消息存储在变量中?请谁能帮我得到这个完整的消息

参考方案

设置$mail->SMTPDebug = 2;后,您需要创建一个回调函数并将其分配给$mail->Debugoutput。在该函数中,您可以将调试输出分配给变量。这是有关此主题的文档:http://phpmailer.github.io/PHPMailer/classes/PHPMailer.PHPMailer.PHPMailer.html#property_Debugoutput

请注意:您的回调函数将在每行调试输出中调用一次(而不是在错误字符串中的所有调试输出中仅调用一次),因此您必须将每行追加到变量中。如果仅分配它,则只会得到调试输出的最后一行,通常与$ mail-> ErrorInfo或异常中的信息相同。

我喜欢做这样的事情:

$GLOBALS['debugOutput'] = [];

$mail->Debugoutput = function($debugOutputLine, $level) {
    $GLOBALS['debugOutput'][] = $debugOutputLine;
};

//...Put your mail code here that could cause an error

$debug_output = implode("\n", $GLOBALS['debugOutput']);
echo $debugOutput;

这应该打印出与示例相同的信息。

php Singleton类实例将在多个会话中保留吗? - php

举一个简单的例子,如果我想计算一个不使用磁盘存储的脚本的命中次数,我可以使用静态类成员来执行此操作吗?用户1:<?php $test = Example::singleton(); $test->visits++; ?> 用户2:<?php $test = Example::singleton(); $test->visits+…

PHP:检查谁看过发送的电子邮件? - php

我正在向某些用户发送电子邮件,并且想知道是谁阅读的,这意味着如果有人阅读了该电子邮件,则将维护一个日志文件,其中包含该用户的电子邮件地址以及日期/时间/ IP。为此,我发送一个带有电子邮件(html模板)的javascript函数,当用户打开该电子邮件时,它仅会警告用户的电子邮件地址,例如:for($n=0; $n<sizeof($checkBox);…

PHP:填写数组中的“空白” - php

我有一个php数组(通过表单中的复选框值获取-您知道复选框仅在未设置时显示在_POST变量中)。Array ( [2] => 0,2 [3] => 0,3 ) 我需要一种方法来“填补” 0-5之间的差距。所以上面看起来像(用'-1'填充空白。我尝试用填充有'-1'的数组array_merge(),但这没用。Array ( [0] => -1…

PHP:对数组排序 - php

请如何排序以下数组Array ( 'ben' => 1.0, 'ken' => 2.0, 'sam' => 1.5 ) 至Array ( 'ken' => 2.0, 'sam' => 1.5, 'ben' =&…

PHP getallheaders替代 - php

我正在尝试从服务器上的apache切换到nginx。唯一的问题是我在PHP脚本中使用的getallheaders()函数,该函数不适用于Nginx。我已经尝试过用户在getallheaders函数上的php站点上提供的注释,但这并不返回所有请求标头。请告诉我如何解决这个问题。我真的想切换到Nginx。 参考方案 您仍然可以使用它,但是您必须像这里一样重新定义…