ZF-8528: Patch to add SMTP pipelining support and suppression of RCPT exceptions to Zend_Mail_Protocol_Smtp

Issue Type: Patch Created: 2009-12-11T20:33:19.000+0000 Last Updated: 2012-11-20T21:37:47.000+0000 Status: Open Fix version(s): Reporter: Marc Hodgins (mjh_ca) Assignee: None Tags: - Zend_Mail

Related issues: - ZF-10741

Attachments: - Zend_Mail_Protocol_Smtp.patch


h2. Patch overview

The attached patch adds two capabilities to Zend_Mail_Protocol_Smtp:

(1) Option to enable SMTP pipelining (" rel="nofollow">rfc 2920). This greatly speeds up SMTP delivery on high-latency connections or when delivering many emails or recipients as there are less round-trips waiting for server responses. (To enable, use constructor $config option pipelining=true).

<pre class="highlight">
$transport = new Zend_Mail_Protocol_Smtp('', array('pipelining' => true));

(2) Option to suppress exceptions on RCPT commands. Useful if you want a message to proceed to send even if one or more recipients are rejected by the server when the RCPT command is issued (use constructor $config option throwRcptExceptions=false). Retrieve RCPT exceptions via getRcptExceptions() -- returns an array where the keys are the failed recipient address and the values are the stored exceptions -- use $exception->getMessage() to read each SMTP response.

<pre class="highlight">
$config = array('throwRcptExceptions' => false);
$transport = new Zend_Mail_Transport_Smtp('smtphost', $config));

$mail = new Zend_Mail;
$mail->addTo('', 'foo'); /* would normally throw exception */
$mail->addTo('', 'foo');
// iterate through rcpt exceptions
foreach ($transport->getConnection()->getRcptExceptions() as $key => $exception) {
    echo sprintf('Failed to send to %s - server responded "%s"', $key, $exception->getMessage());

// get list of failed recipients
$exceptions       = $transport->getConnection()->getRcptExceptions();
$failedRecipients = array_keys($exceptions);

// get count of failed and successful recipients
$numFailed     = count($exceptions);
$numSuccessful = count($mail->getRecipients()) - $numFailed;

h2. Internals

SMTP pipelining allows batches of commands (as implemented here, MAIL FROM, RCPT TO, and RSET) to be sent without waiting for server response. This has been implemented in Zend_Mail_Protocol_Smtp by having the _expect() function queue expected server responses until a non-pipelining command (i.e. DATA) is issued, at which point all queued server responses are processed in sequence, evaluated against the expected responses.

-Unfortunately there is currently very thin unit tests for the SMTP components of Zend_Mail. As the parent class, Zend_Mail_Protocol_Abstract operates directly on sockets which is a necessity to facilitate TLS encryption features, I do not see an easy way to mock up a test adapter to use for testing. Any suggestions about how to do this to allow better testing of the new functionality would be appreciated.- {color:green}Came up with a way to mock the socket connection for unit testing; now awaiting commit of ZF-10741 and will then add unit tests to this patch{color}

The patch also adds internals to facilitate future improvements related to SMTP extensions. EHLO response (the list of server-supported SMTP extensions) is now parsed and can be queried with $this->_supports(). So, future enhancements could include parsing of ENHANCEDSTATUSCODES (" rel="nofollow">rfc 2034), SIZE (" rel="nofollow">rfc 1870), etc.


Posted by Marc Hodgins (mjh_ca) on 2009-12-11T20:39:42.000+0000

Recommend applying (ZF-8511) before this patch (ZF-8528) as ZF-8511 resolves an issue with Zend_Mail not clearing the receive buffer when throwing an exception. ZF-8528 needs this ability as it internally uses a try/catch block to continue processing after an RCPT error when throwRcptExceptions config option is set to false.

Posted by Marc Hodgins (mjh_ca) on 2010-05-27T11:22:51.000+0000

Satoru, could you please provide comment? This patch was for ZF 1.9.6 and it would be nice to get this applied before we're too far along if you think it is acceptable. Or, I'm happy to make changes if you feel they are needed. Would like to get this into ZF so I can stop patching my local version with every release. Thanks.

Posted by Satoru Yoshida (satoruyoshida) on 2010-06-12T06:23:10.000+0000

Sorry, I have been inactive since last April.

Posted by Marc Hodgins (mjh_ca) on 2010-11-24T09:53:07.000+0000

Linking as depending on ZF-10741. Will refactor this patch and add unit tests once the unit tests on ZF-10741 are committed

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.