Phpmailer: How can I save the sendlog to file without output?

Created on 22 Dec 2016  路  4Comments  路  Source: PHPMailer/PHPMailer

Most helpful comment

It's not quite clear, but I assume you mean you want to capture debug output when SMTPDebug = 2. You can do this by injecting a function into the Debugoutput property, like this:

$mail->SMTPDebug  = 2;
$mail->Debugoutput = function($str, $level) {
    file_put_contents('smtp.log', gmdate('Y-m-d H:i:s'). "\t$level\t$str\n", FILE_APPEND | LOCK_EX);
};

This is described in the source code.

All 4 comments

It's not quite clear, but I assume you mean you want to capture debug output when SMTPDebug = 2. You can do this by injecting a function into the Debugoutput property, like this:

$mail->SMTPDebug  = 2;
$mail->Debugoutput = function($str, $level) {
    file_put_contents('smtp.log', gmdate('Y-m-d H:i:s'). "\t$level\t$str\n", FILE_APPEND | LOCK_EX);
};

This is described in the source code.

Hi Synchro,
I have been playing around with the codes and creating different problems too :v
FILE_APPEND works fine and i like the fact that all error gets stored in smtp.log and i can read the smtp.log file to check if the error causing my code to stop is a wrong password or too many login attempt , the problem is Append keeps adding to the same smtp.log which can be misleading , if the first password attempt was a wrong password and the second attempt generates too many login attempts error .. I would like to know the work arround with this as FILE_OVERWRITE overwrites the whole log and leave just the last line "2019-07-28 12:58:07 2 SMTP Error: Could not authenticate." leaving out important information like "2019-07-28 12:58:07 2 SERVER -> CLIENT: 535 5.7.0 (#AUTH005) Too many bad auth attempts." which is what i need to tell my code the next line of action..

(moved comment to #1795)

One thing that I've done with this log file problem is to append a date to the log file and then change that date at a reasonable interval. So, for one application which does not log very much, I append the year-month to the name (e.g. smtp_2020-06.log) and then start a new file the next month (e.g. smtp_2020-07.log). You could modify this to start a new log file every week or every day if you wanted depending on your needs.

Was this page helpful?
0 / 5 - 0 ratings