ZF-30: add a clearRecipients() method to the Zend_Mail (TRAC#14)

Description

The second suggestion is to add a clearRecipients() method to the Zend_Mail class to optimize the process when sending the same email to several receipients.

My criteria are : no use of the BCC, and I do not want users to see which other users has received the email.

Here is how I do it right now :


require_once('Zend/Mail.php');
require_once 'Zend/Mail/Transport/Smtp.php';                                                   
       
$tr = new Zend_Mail_Transport_Smtp('myMailServer');
$tr->connect();
       
foreach ($emails as $value) {
    $mail = new Zend_Mail();
    $mail->setFrom($from);
    $mail->setBodyText($message);
    $mail->setSubject('this is a test');                   
    $mail->addTo($value);
    $mail->send();
}
       
$tr->disconnect();

Here is my suggestion :


require_once('Zend/Mail.php');
require_once 'Zend/Mail/Transport/Smtp.php';                                                   
       
$tr = new Zend_Mail_Transport_Smtp('myMailServer');
$tr->connect();
$mail = new Zend_Mail();
$mail->setFrom($from);
$mail->setBodyText($message);
$mail->setSubject('this is a test');
       
foreach ($emails as $value) {
    $mail->clearRecipients();                      
    $mail->addTo($value);
    $mail->send();
}

$tr->disconnect();

Comments

Another feature creep likely to be requested: mail merge

Mail merge usually involves similar activities found in template instantiation, except for email the instantiation may be once per email in a list, instead of once per page view for templates used to produce web pages. Consider a common, but simple case, where the beginning of an email includes a greeting with the recipient's name, but the remainder of the email remains identical.

We have discussed this request internally, and decided not to add the feature at this time in anticipation of a proper mail merge/queue module in a future release.

In the meantime, a very easy workaround exists for doing this. First, create the mail object with all information except the recipients list:

$mail = new Zend_Mail(); $mail->setFrom('matthew@zend.com, "Matthew Weier O'Phinney"); $mail->setBodyText('This is the message body');

Then, loop over your recipients, clone the mail object, add recipients to the clone, and send:

foreach ($recpients as $to) { $copy = clone $mail; $copy->addTo($to); $copy->send(); unset($copy); }