PEAR Mail、Mail_Mime 和 headers() 覆盖

发布于 2024-10-16 14:43:03 字数 2907 浏览 5 评论 0原文

我目前正在开发一个提醒 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 <[email protected]>';
$headers['Subject'] = '=?UTF-8?B?'.base64_encode('Subject').'?=';
$headers['Reply-To'] = 'XY <[email protected]>';

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 [email protected]
    $to = '=?UTF-8?B?'.base64_encode($row['firstname'].' '.$row['lastname']).'?= <[email protected]>';
    $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
    }
}   

I'm currently working on a reminder PHP Script which will be called via Cronjob once a day in order to inform customers about smth.

Therefore I'm using the PEAR Mail function, combined with Mail_Mime. Firstly the script searches for users in a mysql database. If $num_rows > 0, it's creating a new Mail object and a new Mail_mime object (the code encluded in this posts starts at this point). The problem now appears in the while-loop.

To be exact: The problem is

$mime->headers($headers, true);

As the doc. states, the second argument should overwrite the old headers. However all outgoing mails are sent with the header ($header['To']) from the first user.

I'm really going crazy about this thing... any suggestions?

(Note: However it's sending the correct headers when calling $mime = new Mail_mime() for each user - but it should work with calling it only once and then overwriting the old headers)

Code:

// 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 <[email protected]>';
$headers['Subject'] = '=?UTF-8?B?'.base64_encode('Subject').'?=';
$headers['Reply-To'] = 'XY <[email protected]>';

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 [email protected]
    $to = '=?UTF-8?B?'.base64_encode($row['firstname'].' '.$row['lastname']).'?= <[email protected]>';
    $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
    }
}   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2024-10-23 14:43:03

没有理由保留旧对象而不创建新对象。
正确使用 OOP 并创建新对象 - 您不知道它们内部如何工作。

There is no reason to keep the old object and not creating a new one.
Use OOP properly and create new objects - you do not know how they work internally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文