PEAR Mail – Checking If Mail Sent

Sharing is Caring

PEAR Mail & mail_mime is absolutely fantastic for sending multi-part mime emails and requires absolutely no effort. Probably, the best part of PEAR Mail & mail_mime is not having to deal with mime boundaries.

I often see incorrect code for handling whether the message successfully sent or not. To check if the message is sent we should do something like the following.

$isSent = $mail->send(...);
if (PEAR::isError($isSent)) {
// error handling goes here
} else {
// successfully sent code goes here.
}

If we simply use the following code, we’ll always have a true condition because $isSent won’t be set to null.

$isSent = $mail->send(...);
if ($isSent) {
// successfully sent code goes here.
//Unfortunately this will always be called
} else {
//code here will never actually be executed.
}

Hope this helps!

Sharing is Caring

Brian is a programmer and technology leader living in Niagara Falls with more than ten years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.