2011-02-09 3 views
0

현재 고객에게 smth에 대해 알리기 위해 하루에 한 번 Cronjob을 통해 호출되는 미리 알림 PHP 스크립트를 작성 중입니다.PEAR Mail, Mail_Mime 및 headers() 덮어 쓰기

따라서 PEAR 메일 기능을 Mail_Mime과 결합하여 사용하고 있습니다. 먼저 스크립트는 mysql 데이터베이스에서 사용자를 검색합니다. $num_rows > 0 인 경우 새로운 Mail 개체와 새로운 Mail_mime 개체 (이 게시물에 포함 된 코드는이 시점부터 시작됩니다)가 생성됩니다. 문제는 이제 while-loop에 나타납니다.

은 정확합니다 : 문제는 문서로

$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 매개 변수가 무시되었습니다. –