我有一个 cron 作业,它向订阅者列表发送电子邮件,在 foreach 循环中一次发送一个,并带有 PDF 附件。我从 cron 脚本中收到了这条消息:
Fatal error: Allowed memory size of 94371840 bytes exhausted (tried to allocate 78643193 bytes)
我需要做些什么来防止这个错误?
另外,我很确定它没有完成发送给所有订阅者,那么我应该如何跟踪它,以便它知道如果它没有发送给每个人,它知道在哪里再次接收?
更新程序:这是一个代码示例:(顺便说一下,我正在使用 Zend 框架)
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();
}
}
更新:我正在重用$transport
变量。我的印象是这是发送给多个订阅者的正确方法,但也许这就是原因?你怎么看?
更新:我添加了一堆打印内存使用语句的日志语句,但我现在真的不知道该怎么做。内存使用量随着每封电子邮件而增加。订阅者列表为 200,它达到 160,然后内存不足。我应该怎么办?