Error Handling With PHPmailer

Error handling with PHPMailer

PHPMailer uses Exceptions. Try to adopt the following code:

require_once '../class.phpmailer.php';

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

try {
$mail->AddReplyTo('name@yourdomain.com', 'First Last');
$mail->AddAddress('whoto@otherdomain.com', 'John Doe');
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo('name@yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}

Get full error message from PHPMailer Exception

After setting $mail->SMTPDebug = 2;, you need to create a callback function and assign it to $mail->Debugoutput. In that function, you can assign the debug output to a variable. Here's the documentation on this subject: http://phpmailer.github.io/PHPMailer/classes/PHPMailer.PHPMailer.PHPMailer.html#property_Debugoutput

Please note: Your callback function will be called once per line of debug output (as opposed to just once with all of the debug output in the error string), so you'll have to append each line to your variable. If you just assign it, you'll only get the last line of debug output, which is often the same information that's in $mail->ErrorInfo or the exception.

I prefer to do something like this:

$GLOBALS['debugOutput'] = [];

$mail->Debugoutput = function($debugOutputLine, $level) {
$GLOBALS['debugOutput'][] = $debugOutputLine;
};

//...Put your mail code here that could cause an error

$debug_output = implode("\n", $GLOBALS['debugOutput']);
echo $debugOutput;

This should print out the same information as in your example.

Problem catching and displaying errors when using phpmailer

You're sending a message twice, and the second time it's outside the try block, so any errors that happen there will not be caught. You're also catching non-existent exceptions. Do it like this:

   $mail->send();
} catch ( Exception | \Exception $e ) {
$_SESSION["mail-failure"] = "Something went wrong. Mail was not sent: " . $e->getMessage();
header("Location: form-errors.php");
exit;
}
$_SESSION["mail-success"] = "Mail sent successfully. Thank you.";
header("Location: form-success.php");
};

clear last exception error from PHPMailer for next loop

Because of the exception at the second address, the line with $mail->clearAddresses() is never reached.

So your third mail wil be sent to both some2@email and some3@email.com, throwing the exception again.

try {
$mail->addAddress($email);
$mail->send(); // try block stops here on exception
$mail->clearAddresses(); // addresses are not cleared
} catch (Exception $e) {
// ...
}

You can call $mail->clearAddresses() outside of the try, catch statement.

try {
$mail->addAddress($email);
$mail->send();
} catch (Exception $e) {
// ...
}
$mail->clearAddresses();

You can also call $mail->clearAddresses() before you call $mail->addAddress($email). That way you will always be sure the addresses are cleared before you add one.

try {
$mail->clearAddresses();
$mail->addAddress($email);
$mail->send();
} catch (Exception $e) {
// ...
}

PHPMailer does not give error message?

Your function sendMail doesn't return a boolean. Since it's an array / object the result is always true in this case. Try printing $response before your if statement and you will see the error.

PHPMailer email not send and not get any error

It would really help to read the docs and base your code on the examples provided, which show how to get info about errors, and show debug output. You're not getting any output because you're not asking for any. From the example in the readme:

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

That will show you the reason for any sending failure. To enable SMTP debug output, do this:

$mail->SMTPDebug = 2;


Related Topics



Leave a reply



Submit