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!

Author: Brian Cline

Brian is a programmer living in Niagara Falls with ten years of development experience. He is passionate about automation, business process re-engineering, and gamification. For the last six years or so, he has been working with Salesforce and JavaScript.

Related Posts

Sharing is Caring