SMTP Debugging

If you are having problems connecting or sending emails through your SMTP server, the SMTP class can provide more information about the processing/errors taking place. Use the debug functionality of the class to see what’s going on in your connections. To do that, set the debug level in your script. For example:

$mail->SMTPDebug = \PHPMailer\PHPMailer\SMTP::DEBUG_SERVER;
$mail->SMTPDebug = 2; //Alternative to above constant
$mail->isSMTP(); // tell the class to use SMTP
$mail->SMTPAuth   = true;                // enable SMTP authentication
$mail->Port       = 25;                  // set the SMTP port
$mail->Host       = "mail.yourhost.com"; // SMTP server
$mail->Username   = "name@yourhost.com"; // SMTP account username
$mail->Password   = "your password";     // SMTP account password

Debug levels

Setting the PHPMailer->SMTPDebug property to these numbers or constants (defined in the SMTP class) results in different amounts of output:

You don’t need to use levels above 2 unless you’re having trouble connecting at all - it will just make output more verbose and more difficult to read.

Note that you will get no output until you call send(), because no SMTP conversation takes place until you do that.

Debug output format

The form that the debug output takes is determined by the Debugoutput property. This has several options:

By default PHPMailer will use echo if run from a cli or cli-server SAPI, html otherwise. Alternatively, you can implement your own system by providing a callable expecting two parameters: a message string and the debug level:

$mail->Debugoutput = function($str, $level) { echo 'debug level ' . $level . '; message: ' . $str; };

You can of course make this as complex as you like - for example you could capture all the output and store it in a database.

And finally, don’t forget to disable debugging before going into production.