我目前正在编写一个提醒 PHP 脚本,该脚本将通过 Cronjob 每天调用一次,以便通知客户有关某事的信息。
因此,我使用 PEAR Mail 功能,并结合Mail_Mime。首先,该脚本在 mysql 数据库中搜索用户。如果$num_rows > 0
,它正在创建一个新Mail
对象和一个新Mail_mime
对象(本文中包含的代码从此时开始)。问题现在出现在 while 循环中。
准确地说:问题是
$mime->headers($headers, true);
作为医生。状态,第二个参数应该覆盖旧的标题。但是,所有外发邮件都带有$header['To']
第一个用户的标头 ( )。
我真的对这件事发疯了……有什么建议吗?
(注意:但是,它在$mime = new Mail_mime()
为每个用户调用时会发送正确的标题 - 但它应该只调用一次然后覆盖旧标题)
代码:
// sql query and if num_rows > 0 ....
require_once('/usr/local/lib/php/Mail.php');
require_once('/usr/local/lib/php/Mail/mime.php');
ob_start();
require_once($inclPath.'/email/head.php');
$head = ob_get_clean();
ob_start();
require_once($inclPath.'/email/foot.php');
$foot = ob_get_clean();
$XY['mail']['params']['driver'] = 'smtp';
$XY['mail']['params']['host'] = 'smtp.XY.at';
$XY['mail']['params']['port'] = 25;
$mail =& Mail::factory('smtp', $XY['mail']['params']);
$headers = array();
$headers['From'] = 'XY <service@XY.at>';
$headers['Subject'] = '=?UTF-8?B?'.base64_encode('Subject').'?=';
$headers['Reply-To'] = 'XY <service@XY.at>';
ob_start();
require_once($inclPath.'/email/templates/files.mail.require-review.php');
$template = ob_get_clean();
$crfl = "\n";
$mime = new Mail_mime($crfl);
while($row = $r->fetch_assoc()){
$html = $head . $template . $foot;
$mime->setHTMLBody($html);
#$to = '=?UTF-8?B?'.base64_encode($row['firstname'].' '.$row['lastname']).'?= <'.$row['email'].'>'; // for testing purpose i'm sending all mails to webmaster@XY.at
$to = '=?UTF-8?B?'.base64_encode($row['firstname'].' '.$row['lastname']).'?= <webmaster@XY.at>';
$headers['To'] = $to; // Sets to in headers to a new
$body = $mime->get(array('head_charset' => 'UTF-8', 'text_charset' => 'UTF-8', 'html_charset' => 'UTF-8'));
$hdrs = $mime->headers($headers, true); // although the second parameters says true, the second, thrid, ... mail still includes the To-header form the first user
$sent = $mail->send($to, $hdrs, $body);
if (PEAR::isError($sent)) {
errlog('error while sending to user_id: '.$row['id']); // personal error function
} else {
// Write log file
}
}