简体   繁体   English

PHP&Pear ::邮件内存耗尽

[英]PHP & Pear::Mail Memory Exhaustion

Solution below 解决方案如下

Hi guys. 嗨,大家好。 I've been trying different ways to approach this but still hitting the same error. 我一直在尝试不同的方法来解决此问题,但仍然遇到相同的错误。 I have a form where you can select some users email addresses and some pdf file and it will send to them. 我有一个表格,您可以在其中选择一些用户的电子邮件地址和一些pdf文件,它将发送给他们。 Problem is that PHP will throw an error because the script is using massive amounts of memory (over 90 meg). 问题是PHP将引发错误,因为脚本正在使用大量内存(超过90兆)。 I have tried using mail() and now trying PEAR:Mail_Mime Is there another way I can do this? 我尝试使用mail(),现在尝试PEAR:Mail_Mime还有另一种方法可以做到这一点吗?


include_once('Mail.php');
include_once('Mail/mime.php');
$from = "it@example.com";
$subject = $_POST[subject];
$text = $_POST[message];

if (count($_POST[emailEnq]) > 0) {
 foreach ($_POST[emailEnq] as $Ekey => $Evalue) {
  $message = new Mail_mime();
  $message->setTXTBody($text);
  if (count($_POST[emailFile]) > 0) {
   foreach ($_POST[emailFile] as $key => $value) {
    $filepath = "/home/mds07/console/admin/media/listings/" . $_POST[list_ID] . "/docs/";
    ////////////////////You will need to change the above line if the location of the PHP program ever moves////////////////////////////
    $fileatt = $filepath . $value;
    $message->addAttachment($fileatt);
   }
  }
  $body = $message->get();
  $extraheaders = array("From" => $from, "Subject" => $subject);
  $headers = $message->headers($extraheaders);
  $mail = Mail::factory("mail");
  $mail->send($Evalue, $headers, $body);
 }
}

SOLUTION
The following code is working with much better memory usage: 以下代码可以更好地利用内存:


$from = "it@example.com";

echo 'From: '.$from."\n"; echo 'Subject: '.$_POST[subject]."\n"; echo 'Text: '.$_POST[message]."\n";

include_once('Mail.php'); include_once('Mail/mime.php'); $message = new Mail_mime(); $message->setTXTBody($_POST[message]); if (count($_POST[emailFile]) > 0) { foreach ($_POST[emailFile] as $key => $filename) { $filepath = "/home/mds07/console/admin/media/listings/" . $_POST[list_ID] . "/"; ////////////////////You will need to change the above line if the location of the PHP program ever moves//////////////////////////// $fileatt = $filepath . $filename; $message->addAttachment($fileatt); echo 'Attached File: '.$filename."\n"; } } $body = $message->get(); $extraheaders = array("From" => $from, "Subject" => $_POST[subject]); $headers = $message->headers($extraheaders); $mail = Mail::factory("mail"); if (count($_POST[emailEnq]) > 0) { foreach ($_POST[emailEnq] as $key => $recipient) { $mail->send($recipient, $headers, $body); echo 'Sent mail to: '.$recipient."\n"; } }

Without having used Pear::Mail_Mime I'd bet you run out of memory because you create a brand new Mail_Mime() object on every loop iteration, when it seems like it's the same in all of them. 如果不使用Pear :: Mail_Mime,我敢打赌您会用光内存,因为您似乎在所有循环中都一样,因此在每次循环迭代时都会创建一个全新的Mail_Mime()对象。

Create as few as possible, taking it out of the outermost foreach. 尽可能少创建,将其从最外层的foreach中删除。

Same for $mail . $mail相同。 You can probably re-use the same $mail object and feed it new headers and body. 您可能可以重复使用相同的$mail对象,并为其提供新的标头和正文。

To make sure where your memory is used, surround the suspect lines with memory_get_usage() and log its output to see the increase. 为了确保在哪里使用内存,请使用memory_get_usage()包围可疑行,并记录其输出以查看增加的情况。

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

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