无法检索完整的SMTP错误日志PHP邮件


Unable to retrieve full SMTP error log PHP mailer

我创建了一个简单的表log,如果邮件无法发送,我将在其中插入记录。

mysql> describe log;
+--------+-----------+------+-----+---------------------+-----------------------------+
| Field  | Type      | Null | Key | Default             | Extra                       |
+--------+-----------+------+-----+---------------------+-----------------------------+
| id_log | int(11)   | NO   | PRI | NULL                | auto_increment              |
| error  | text      | YES  |     | NULL                |                             |
| time   | timestamp | NO   |     | 0000-00-00 00:00:00 | on update CURRENT_TIMESTAMP |
+--------+-----------+------+-----+---------------------+-----------------------------+

在我的PHP端,错误记录是这样的:

  foreach ($arrayWithMails as $key => $value) {
        $mail->addAddress($value);
        if (!$mail->send())
        {
            put('message', $mail->ErrorInfo); //store in session
//            echo $mail->ErrorInfo;
        }
        else {
            /* Add email from array in session to be displayed at main page */
            put('emailAddresses', $arrayWithMails); //Store in session
        }
        $mail->ClearAllRecipients();
    }
$log->logError($_SESSION['message']);

它在我的类中调用一个方法,该方法将字符串作为参数传递,并插入到DB中。

然而,我所有的日志都是这样的:

mysql> select * from  log;
+--------+------------------------------------------------------------------------------------+---------------------+
| id_log | error                                                                              | time                |
+--------+------------------------------------------------------------------------------------+---------------------+
|      1 | SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting | 2016-10-21 15:08:30 |
|      2 | 1                                                                                  | 2016-10-21 15:12:04 |
|      3 | 1                                                                                  | 2016-10-21 15:13:42 |
|      4 | SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting | 2016-10-21 15:14:14 |
|      5 | SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting | 2016-10-21 15:17:02 |
|      6 | SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting | 2016-10-21 15:18:22 |
|      7 | SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting | 2016-10-21 15:28:02 |
|      8 | SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting | 2016-10-21 15:28:53 |
+--------+------------------------------------------------------------------------------------+---------------------+

我的SMTP调试设置为4。用2也试过,结果相同。

如果f.e.我做$mail->ErrorInfo的基本回声,我得到完整的日志,有错误,但是当我想在DB中存储完整的日志信息时,我只得到:SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting如果我试图将错误存储在$_SESSION var中,也会发生同样的事情。

知道如何存储完整的日志吗?谢谢!

ErrorInfo只包含错误消息,不包含完整的调试日志。调试输出就是这样——默认情况下,它被转储到stdout中,而不是保留。

要捕获它,您需要查看Debugoutput属性-在PHPMailer的最新版本中,您可以将其设置为可调用的,您可以使用它来收集调试输出并将其插入到数据库中。

例如:

//Before sending
$debuglog = '';
$mail->Debugoutput = function($str, $level) use ($debuglog) {
    $debuglog .= $str;
};
//... after calling send()
$log->logError($mail->Errorinfo . ' ' . $debuglog);

这就是我的答案。我正在扩展PHPMailer,我已经包含了它会抛出异常,所以这样我就能正确地得到异常消息。

foreach ($array as $key => $value) {
    $mail->addAddress($value);
    try
    {
        if (!$mail->send()) {
            put('message', $mail->ErrorInfo); // Storing for frontend, that error exist, for detailed info - log is in DB
        }
        else {
            /* Add email from array in session to be displayed at main page */
            put('emailAddresses', $array);
        }
    } catch (phpmailerException $exc)
    {
        $debugError = $exc->errorMessage();
    }
    $mail->ClearAllRecipients();
}
$log->logError($debugError);