如何从 phpmailer 中记录完整的错误信息?
How to log the full errorinfo from phpmailer?
我使用 phpmailer 发送邮件,我想在发生错误时记录来自 phpmailer 的错误。我只能得到一部分错误,不能得到全部错误。
对于日志记录,我使用的是独白。
$log = new Logger('contact.php');
$log->pushHandler(new StreamHandler('./error.log',logger::WARNING));
正如@Synchro 所建议的那样,我尝试使用闭包来捕获整个调试过程。
if (!$err) {
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'maildev';
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->Debugoutput = 'html';
$mail->SMTPAutoTLS = false;
$mail->Port = '259';
$mail->CharSet = 'utf-8';
//$mail->setFrom($config['mail']['sendTo'], (empty($name) ? 'Contact form' : $name));
$mail->setFrom($config['mail']['sendTo']);
$mail->addAddress($config['mail']['sendTo']);
$mail->addReplyTo($email, $name);
$mail->isHtml();
$mail->Subject = 'Contact form: ' . $subject;
$mail->Body = $query;
$result="Thank you for connecting with us! We'll reply as soon as possible to you.";
$form->clearPost();
try {
$debuglog = '';
$mail->Debugoutput = function($str, $level) use ($debuglog) {
$debuglog .= $str;
};
if (!$mail->send()) {
$msg .= "Mailer Error: " . $mail->ErrorInfo;
array_push($extra_info,['name',$name,$email,$query]);
$log->error($debuglog, $extra_info);
} else {
$msg .= "Message sent!";
$result="Thank you for connecting with us! We'll reply as soon as possible to you.";
}
} catch (phpmailerException $exp) {
$debugError = $exp->errorMessage();
}
$log->error($debugError);
$debugError 和 $debuginfo 均为 NULL。
Errorinfo 在浏览器中产生下一个输出:
2019-08-20 14:10:53 Connection: opening to maildev:259, timeout=300, options=array()
2019-08-20 14:10:53 Connection failed. Error #2: stream_socket_client(): unable to connect to maildev:259 (Connection refused) [/srv/http/vendor/phpmailer/phpmailer/src/SMTP.php line 326]
2019-08-20 14:10:53 SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
但在日志文件中我只得到:
[2019-08-20 14:10:53] contact.php.ERROR: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting ["172.27.0.1",["name","t","t@q.nl","werty"]] []
如何记录完整的调试消息而不是只记录 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
?
PHPMailer 生成的调试输出与 ErrorInfo
中的最终错误消息是分开的 - 例如,即使没有错误,调试输出也会存在。
如果你想捕获所有的调试输出,你可以注入一个闭包,它是documented in the source这样的:
$mail->Debugoutput = function($str, $level) use ($log) {
$log->error("debug level $level; message: $str");
};
你不用说 $log
里是什么,但如果是 a PSR-3 logger,你可以简单地这样做:
$mail->Debugoutput = $log;
请注意,这将始终产生输出 - 如果您想要它的一个子集,您可以在将其发送到您的日志之前对其进行过滤。
我使用 phpmailer 发送邮件,我想在发生错误时记录来自 phpmailer 的错误。我只能得到一部分错误,不能得到全部错误。
对于日志记录,我使用的是独白。
$log = new Logger('contact.php');
$log->pushHandler(new StreamHandler('./error.log',logger::WARNING));
正如@Synchro 所建议的那样,我尝试使用闭包来捕获整个调试过程。
if (!$err) {
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'maildev';
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->Debugoutput = 'html';
$mail->SMTPAutoTLS = false;
$mail->Port = '259';
$mail->CharSet = 'utf-8';
//$mail->setFrom($config['mail']['sendTo'], (empty($name) ? 'Contact form' : $name));
$mail->setFrom($config['mail']['sendTo']);
$mail->addAddress($config['mail']['sendTo']);
$mail->addReplyTo($email, $name);
$mail->isHtml();
$mail->Subject = 'Contact form: ' . $subject;
$mail->Body = $query;
$result="Thank you for connecting with us! We'll reply as soon as possible to you.";
$form->clearPost();
try {
$debuglog = '';
$mail->Debugoutput = function($str, $level) use ($debuglog) {
$debuglog .= $str;
};
if (!$mail->send()) {
$msg .= "Mailer Error: " . $mail->ErrorInfo;
array_push($extra_info,['name',$name,$email,$query]);
$log->error($debuglog, $extra_info);
} else {
$msg .= "Message sent!";
$result="Thank you for connecting with us! We'll reply as soon as possible to you.";
}
} catch (phpmailerException $exp) {
$debugError = $exp->errorMessage();
}
$log->error($debugError);
$debugError 和 $debuginfo 均为 NULL。
Errorinfo 在浏览器中产生下一个输出:
2019-08-20 14:10:53 Connection: opening to maildev:259, timeout=300, options=array()
2019-08-20 14:10:53 Connection failed. Error #2: stream_socket_client(): unable to connect to maildev:259 (Connection refused) [/srv/http/vendor/phpmailer/phpmailer/src/SMTP.php line 326]
2019-08-20 14:10:53 SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
但在日志文件中我只得到:
[2019-08-20 14:10:53] contact.php.ERROR: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting ["172.27.0.1",["name","t","t@q.nl","werty"]] []
如何记录完整的调试消息而不是只记录 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
?
PHPMailer 生成的调试输出与 ErrorInfo
中的最终错误消息是分开的 - 例如,即使没有错误,调试输出也会存在。
如果你想捕获所有的调试输出,你可以注入一个闭包,它是documented in the source这样的:
$mail->Debugoutput = function($str, $level) use ($log) {
$log->error("debug level $level; message: $str");
};
你不用说 $log
里是什么,但如果是 a PSR-3 logger,你可以简单地这样做:
$mail->Debugoutput = $log;
请注意,这将始终产生输出 - 如果您想要它的一个子集,您可以在将其发送到您的日志之前对其进行过滤。