繁体   English   中英

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

[英]Zend Framework: Fatal error when trying to use Zend Mail Transport to send multiple emails with attachments

我不完全理解这一切是如何工作的,但我收到了这个错误:

致命错误:第213行的/Users/andrew/Sites/myApp/library/Zend/Mail/Transport/Smtp.php中允许的内存大小为8388608字节(试图分配261858字节)

我在运行MAMP的Mac上本地运行此代码。 不确定这是否与它有关。 这是我的代码,基本上:

$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);
    }

但是,订阅者越多,这个数字最终得到的越多,这个修复只会帮助这么久:

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

我需要弄清楚如何发送一封带有附件的电子邮件给几百个人。 这是我想出来的其他东西,但是只设置密件抄送而不是地址似乎有点笨拙:

$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);

但是,即使这样做,我也需要指定“memory_limit”。 你能指点我正确的方向吗? 有什么我不做的事吗?

我猜你的pdf大概是250K字节? 您发送的每封电子邮件都会将其读入内存。 别。 阅读一次。 :)它也可能是Zend框架中的编码事物。

  • 在循环之前调用file_get_contents() 一次
  • 只要您的服务器可以处理它就设置内存限制更高(我说的是32-128 MB)
  • unset()你的变量 - 应该强制php到GC它(理论上)
  • 您可以重用$ message对象(丑陋的黑客,但如果Zend执行某种文件编码并且它使用大量内存,则可以节省字节)

我还会发送电子邮件,并确保每封电子邮件(或对其的引用)与状态一起存储在数据库中。 这样,如果您遇到另一个内存限制或错误,您将不会发送重复的邮件。

无需为每条消息创建新附件。 只需创建一次,然后在每次发送时附加它。

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

$attachment = new Zend_Mime_Part(file_get_contents($filepath));
$attachment->type = 'application/pdf';
$attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
$attachment->filename = $filename;

foreach ($subscribers as $subscriber) {
    $message = new Zend_Mail('utf-8');
    $message->setFrom('my@mailinglist.com', 'Mailing List')
            ->addTo($subscriber->email)
            ->setSubject($subject)
            ->setBodyText($body);
    $message->addAttachment($attachment);
    $message->send($smtpConnection);
}

我有内存限制的类似问题,并通过一个SMTP连接发送大量的消息。 Zend_Mail_Protocol_Abstract将其内部日志保存在内存中。 日志中记录了所有邮件请求。 日志随着每条消息的发送而成长。 你有时需要调用$ protocol-> resetLog()。 (这取决于每条消息的数据量。您可以通过memory_get_usage()PHP函数检查内存使用情况。)尝试这样的事情:

  $transport = new Zend_Mail_Transport_Smtp();
  $protocol = new Zend_Mail_Protocol_Smtp('localhost');
  $protocol->connect();
  $protocol->helo('localhost');
  $transport->setConnection($protocol);
  foreach(){
    $mail = new Zend_Mail('utf-8');
    ...
    $protocol->rset();
    $mail->send($transport);
    $protocol->resetLog();  // you don't need to resetLog for each message
  }

这也可能有用: http//framework.zend.com/manual/en/zend.mail.multiple-emails.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM