2011-02-09 99 views
0

我目前正在研究一個提醒PHP腳本,它將通過Cronjob每天調用一次,以告知客戶有關smth。PEAR Mail,Mail_Mime和標題()覆蓋

因此,我使用了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 
    } 
} 

回答

1

有沒有理由保留舊對象而不創建新對象。 正確使用OOP並創建新對象 - 你不知道它們是如何在內部工作的。

+0

問題是服務器上的舊版本,由於一些非常奇怪的配置問題,忽略了$ overwrite參數。 – 2011-04-16 12:05:48