I have a cron job that sends emails to a list of subscribers, one at a time in a foreach loop, with a PDF attachment. I got this message from the cron script:
Fatal error: Allowed memory size of 94371840 bytes exhausted (tried to allocate 78643193 bytes)
What do I need to do to prevent this error?
Also, I'm pretty sure that it didn't finish sending to all the subscribers, so how should I keep track of this so it knows where to pick up again if it didn't send to everyone?
Updater: Here's a code sample: (I'm using the Zend Framework by the way)
public function send(Default_Model_MyEmail $myEmail)
{
if (null != ($id = $myEmail->attachmentId)) {
$file = new Default_Model_File();
$file->find($id);
$filepath = APPLICATION_UPLOADS_DIR . '/' . $file->getActualFilename();
$attachment = new Zend_Mime_Part(file_get_contents($filepath));
$attachment->type = $file->getMimeType();
$attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
$attachment->encoding = Zend_Mime::ENCODING_BASE64;
$attachment->filename = $file->getDisplayFilename();
}
$transport = new Zend_Mail_Transport_Smtp('localhost');
$mail = new Zend_Mail('utf-8');
$mail->setFrom('from@address', 'From Name');
$mail->setReplyTo('replyto@address');
$mail->setSubject($myEmail->subject);
if (isset($attachment)) {
$mail->addAttachment($attachment);
}
$subscribers = $this->getSubscribers();
foreach ($subscribers as $subscriber) {
$mail->addTo($subscriber->email);
$bodyText = $myEmail->body
. "\r\n\r\nIf for any reason you would like to be removed from this mailing list, "
. "please visit \r\nhttp://myapp.com/myemail/unsubscribe/email/"
. $subscriber->email;
$mail->setBodyText($bodyText);
$mail->send($transport);
$mail->clearRecipients();
}
}
Update: I am reusing the $transport
variable. I was under the impression this was the correct way to send to multiple subscribers, but maybe this is the cause? What do you think?
Update: I've added a bunch of log statements that print memory usage statements, but I don't really know what to do now. The memory usage increases with every email. With a subscriber list of 200, it gets to 160 then runs out of memory. What should I do?