douyou8266 2009-04-23 11:49
浏览 107
已采纳

Zend Framework:尝试使用Zend Mail Transport发送带附件的多封电子邮件时出现致命错误

I don't totally understand how all this works, but I'm getting this error:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 261858 bytes) in /Users/andrew/Sites/myApp/library/Zend/Mail/Transport/Smtp.php on line 213

I'm running this code locally on my Mac running MAMP. Not sure if that has anything to do with it. This is my code, basically:

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

    foreach ($subscribers as $subscriber) {
        $message = new Zend_Mail('utf-8');
        $message->setFrom('my@mailinglist.com', '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);
    }

However, the more subscribers there are, the higher this number ends up getting, and this fix will only help for so long:

ini_set("memory_limit","12M");

I need to figure out how to send an email with an attachment to a couple hundred people. Here's something else I've come up with but it seems a little hacky to only set the bcc and not the to address:

$message = new Zend_Mail('utf-8');
    $message->setFrom('my@mailinglist.com', 'Mailing list')
            ->setSubject($subject)
            ->setBodyText($body);
    $attachment = $message->createAttachment(file_get_contents($filepath));
    $attachment->type = 'application/pdf';
    $attachment->filename = $filename;

    foreach ($subscribers as $subscriber) {
        $message->addBcc($subscriber->email);
    }
    $message->send($smtpConnection);

However, even doing this, I need to specify the "memory_limit". Can you please point me in the right direction with this? Is there something I'm not doing?

  • 写回答

3条回答 默认 最新

  • dpd7195 2009-04-23 12:31
    关注

    I'm guessing your pdf is about 250Kbytes? You're reading it into memory once per email you send out. Don't. Read it once. :) It might also be an encoding-thing in the Zend framework.

    • Call file_get_contents() once before your loop
    • Set the memory limit much higher as long as your server can handle it (I'd say along the lines of 32-128 Mbytes)
    • unset() your variables - should force php to GC it (in theory)
    • You could reuse the $message object (ugly hack, but could save bytes if Zend does some sort of file-encoding and it uses lots of memory)

    I'd also make a cron-job for sending the emails, and making sure that each email (or a reference to it) is stored in the database along with a status. This way you won't send duplicate mails if you hit another memory limit, or bug.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测