How to verify a mail has been sent when using Zend_Mail?

Gary Willoughby picture Gary Willoughby · Oct 15, 2009 · Viewed 11.6k times · Source

I am using the Zend framework to send mail. Once the config is done and the code written it all boils down to one call:

$Mail->send($Transport)

How can i check that this mail has been sent correctly? I read somewhere that Zend Mail throws an exception but other people have said this is sometimes not the case.

What's the bulletproof programmatic way to ensure mail has been sent properly when using Zend_Mail?

EDIT: When i mean sent, i mean sent to the SMTP server.

Answer

Stefan Gehrig picture Stefan Gehrig · Oct 15, 2009

Generally Zend_Mail will throw an exception if there is something wrong going on on the send-process - but this strongly depends on the Zend_Mail_Transport_* being used.

You have two options here:

  • Zend_Mail_Transport_Sendmail (the default transport) relies on mail(). If mail() returns false, Zend_Mail_Transport_Sendmail throws a Zend_Mail_Transport_Exception (Unable to send mail). The return value itself is not very reliable. This is what the manual says about the return value:

    Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

    It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

  • Zend_Mail_Transport_Smtp sends the email using the SMTP protocol that's encapsulated in Zend_Mail_Protocol_Smtp. In this case you'll get a Zend_Mail_Protocol_Exception whenever something either violates the SMTP protocol (sending mail without giving a sender's address e.g.) or the STMP server reports an error or the connection times out.

    So if no exception is thrown when talking to the STMP server, you can be sure that the remote server at least accepted your email.