PHP and PHPMailer: How to Check if Your Mail Send Failed CodeUnit 21 DEC 2011

PHPMailer is a great little mail sending workhorse class for PHP, widely expanding on the regular PHP mail() function and particularly easy to make use of, making it a personal favourite of mine.

Anyway, to check whether or not your mail send was successful when using PHPMailer, all you need to do is check the Boolean variable returned by the Send() function, and if false, check what the error message contained in ErrorInfo is.

In practice:

$mail = new PHPMailer();
$mail->Host = ";";
$mail->SMTPAuth = true;
$mail->Username = 'smtpusername';
$mail->Password = 'smtppassword';

$mail->Subject = "Test 1";
$mail->Body = "Test 1 of PHPMailer.";

   echo "Error sending: " . $mail->ErrorInfo;
   echo "E-mail sent";

As simple as that! :)

About Craig Lotter

Software developer, husband and dad to two young ladies. Writer behind An Exploring South African. I don't have time for myself any more.