【发布时间】:2011-08-22 13:02:09
【问题描述】:
好的,这是我第一次在这里提问。我的问题很尴尬,很难深入到底。 故事是这样的:我有一个小系统,它会发送很多电子邮件邀请(不是垃圾邮件)。所以,明智的,我不使用 PHP 函数 mail(), 我使用 PEAR 类,如 Mail、Mail_Queue、Net_SMTP 等。 唯一的问题是,我的错误日志充满了大量这样的错误:
PHP Notice: Error in sending mail:
Mail Queue Error: Cannot initialize container in /usr/lib/php/PEAR.php on line 873
然后,当然:
[18-Feb-2011 17:38:44] PHP Fatal error:
Allowed memory size of 33554432 bytes exhausted
(tried to allocate 3 bytes) in /usr/lib/php/PEAR.php on line 844
这是初始化邮件队列的代码(在一个名为 Newsletter 的类中)
//I know passing the DSN as the string is kind of deprecated,
//but it;'s the only way it works on my system
$dsn ="mysql://$db_user:$db_pass@$db_host/$db_name";
$db_options = array();
$db_options['type'] = 'db';
$db_options['dsn'] = $dsn;
$db_options['mail_table'] = TABLE_QUEUE;
$this->host = '-- valid host here --';//data in these strings has been obscured
$this->username = '-- valid username here --';
$this->password = '-- valid password here --';
//optionally, a 'dns' string can be provided instead of db parameters.
//look at DB::connect() method or at DB or MDB docs for details.
//you could also use mdb container instead db
//$server = isset($_SERVER['SERVER_NAME'])?$_SERVER['SERVER_NAME']:'localhost';
$mail_options = array(
'driver' => 'smtp',
'host' => $this->host,
'port' => 25,
'auth' => true,
'username' => $this->username,
'password' => $this->password,
);
$this->mail_queue = new Mail_Queue($db_options, $mail_options);
还有一些代码,
public function sendChunk($start, $count)
{
global $db;
$ids = $db->get_results("SELECT DISTINCT id_user as id FROM ".TABLE_QUEUE);
$ret = array();
foreach ($ids as $id)
$ret[] = $id->id;
unset($ids);
$this->mail_queue->sendMailsInQueue($count, $start, 1);
return true;
}
问题是,我仔细检查了我写的每一行代码,它正在做它的工作。唯一的问题是有时它拒绝发送任何邮件。 提前感谢您的回复。
【问题讨论】:
-
mail_queue 中的某些东西可能会随着时间的推移而累积并占用所有可用内存。不要在整个邮件运行过程中使用相同的对象,而是尝试在发送一定数量的电子邮件后切换到创建新的 mail_queue?这应该刷新任何正在建立的东西并定期释放一些内存。
标签: php email pear mail-queue