Reputation: 59
I use phpmailer to send mails and I would like to log the error from phpmailer when it occurs. I can only get a part of the error and not the whole error.
For logging I'm using monolog.
$log = new Logger('contact.php');
$log->pushHandler(new StreamHandler('./error.log',logger::WARNING));
As @Synchro suggested I've tried to use closure to capture the whole debug.
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 and $debuginfo are both NULL.
Errorinfo produces the next output in the browser:
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
But in the log file I get only:
[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","[email protected]","werty"]] []
How can I log the full debugging message instead of only SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
?
Upvotes: 0
Views: 3853
Reputation: 37750
PHPMailer produces debug output that is separate from the final error message that's in ErrorInfo
- for example debug output is present even when there are no errors.
If you want to capture all the debug output, you can inject a closure, which is documented in the source like this:
$mail->Debugoutput = function($str, $level) use ($log) {
$log->error("debug level $level; message: $str");
};
You don't say what is in $log
, but if it's a PSR-3 logger, you can simply do this:
$mail->Debugoutput = $log;
Note that this will always produce output - if you want a subset of it it's up to you to filter it before sending it to your log.
Upvotes: 1