views:

259

answers:

3

I think I must be doing something wrong here because my code only sent an email to the last subscriber in the table. When I log the array of subscribers, its obvious that there are more than one that it is trying to send to. I think the problem has to do with trying to batch them together...What is the best way for me to do this? I'm trying to create one message, with an attachment, then send address each one individually, and send them all out as one batched process. Here's my code:

 $subscribersManager = new DD_Subscribers_Manager();
 $subscribers = $subscribersManager->getAllSubscribers();


 $subject = $form->getElement('subject')->getValue();
 $body = $form->getElement('body')->getValue();
 $filename = $form->getElement('bulletin')->getValue();
 $filepath = Zend_Registry::get('rootDir') . '/public/downloads/archive/' . $filename;

 $config = array('ssl' => 'tls', 'port' => 587, 'auth' => 'login', 'username' => '[email protected]', 'password' => 'password');
 $smtpConnection = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);

 foreach ($subscribers as $subscriber) {
  $message = new Zend_Mail('utf-8');
  $message->setFrom('[email protected]', 'My Fake Mailing List')
    ->addTo($subscriber->email)
    ->setSubject($subject)
    ->setBodyText($body);
  $attachment = $message->createAttachment(file_get_contents($filepath));
  $attachment->type = 'application/pdf';
  $attachment->filename = $filename;
 }
 $message->send($smtpConnection);
+2  A: 

Looks to me like you're creating one object for each subscriber, and then not doing anything with any of them except the last. Move the $message->send call inside the foreach loop.

Graeme Perrow
+4  A: 

This looks like a scope issue: You create $message inside the foreach loop and only send the last one (since $message->send() is called after the foreach.

Basically, you overwrite the value of $message each time the foreach loop iterates. Move $message->send into the foreach loop to send one email per iteration. There may be another way to create an array of messages and send them all at once, but I'm not familiar with ZendMail.

EDIT: Actually, if you read the documentation on Zend_Mail you can call $message->addTo() to add additiional recipients (more than 1) to a single message. Your code could change to something like:

$message = new Zend_Mail();
$message->setFrom('[email protected]', 'My Fake Mailing List')
                                ->setSubject($subject)
                                ->setBodyText($body);

foreach($subscribers as $subscriber){

    $message->addTo($subscriber->email);
}
$message->send();

Zend_Mail - adding recipients

Brett Bender
A: 

put the "send" method calling code inside the foreach loop

$message->send($smtpConnection); as shown below

foreach ($subscribers as $subscriber) { $message = new Zend_Mail('utf-8'); $message->setFrom('[email protected]', 'My Fake Mailing List') ->addTo($subscriber->email) ->setSubject($subject) ->setBodyText($body); $attachment = $message->createAttachment(file_get_contents($filepath)); $attachment->type = 'application/pdf'; $attachment->filename = $filename; $message->send($smtpConnection);

}

Jaya