簡體   English   中英

PHP&Pear ::郵件內存耗盡

[英]PHP & Pear::Mail Memory Exhaustion

解決方案如下

嗨,大家好。 我一直在嘗試不同的方法來解決此問題,但仍然遇到相同的錯誤。 我有一個表格,您可以在其中選擇一些用戶的電子郵件地址和一些pdf文件,它將發送給他們。 問題是PHP將引發錯誤,因為腳本正在使用大量內存(超過90兆)。 我嘗試使用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);
 }
}


以下代碼可以更好地利用內存:


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

如果不使用Pear :: Mail_Mime,我敢打賭您會用光內存,因為您似乎在所有循環中都一樣,因此在每次循環迭代時都會創建一個全新的Mail_Mime()對象。

盡可能少創建,將其從最外層的foreach中刪除。

$mail相同。 您可能可以重復使用相同的$mail對象,並為其提供新的標頭和正文。

為了確保在哪里使用內存,請使用memory_get_usage()包圍可疑行,並記錄其輸出以查看增加的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM