2016-03-03 74 views
1

我有一個腳本每月運行一次cron作業,向客戶發送awstats報告。我們最近不得不修改它,因爲我們的出站郵件服務器收緊了限制。不幸的是,一些收件人在郵件正文中以原始html代碼的形式接收報告。任何人都可以從劇本中知道我出錯的地方嗎?電子郵件腳本格式不正確

########################## 
## Call to get awstats 
########################## 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL,"http://www.$your_domain:2082/awstats.pl" 
     ."?month=$report_month&year=$report_year&output=main&config=$your_domain" 
     ."&lang=en&framename=mainright"); 
curl_setopt($curl, CURLOPT_USERPWD, "$user:$password"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
//$attachment = curl_exec($curl); 
$attachment = "<br /><br /><div align=\"center\"><a href=\"$logo_link\"><img src=\"$logo_path\" border=\"0\" /></a>\n"; 
$attachment .= "<br /><font color=\"#FF0000\"><b>AwStats Report for $your_domain ($report_monthname $report_year)</b></font></div>\n"; 
$attachment .= "<base href=\"$awstats_img_path\">\n"; 
$attachment .= curl_exec($curl); 
curl_close($curl); 

########################## 
## Call to send email 
########################## 

$subject = "AwStats Report for $your_domain ($report_monthname $report_year)"; 
$header = "From: " . $email_from . " <" . $email_from . ">\n"; 
$header .= "Reply-To: " . $email_from . "\n"; 
$header .= "MIME-Version: 1.0\n"; 
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\n\n"; 
$uid = md5(uniqid(time())); 
$message = "--" . $uid . "\n"; 
$message .= "Content-type:text/html; charset=iso-8859-1\n"; 
$message .= "Content-Transfer-Encoding: 7bit\n\n"; 
$message .= $attachment . "\n\n"; 
mail($email_to, $subject, $message, $header); 

我明顯地忽略了這裏的變量聲明。但他們都在代碼中。我實際上收到了一條消息,並且它在桌面上的Apple Mail中顯示得很好。

感謝, CJ

+0

哪裏?在前景? – devpro

+0

客戶端可能正在使用outlook。我正在使用Mac Mail。 –

回答

1

你缺少的,這將導致一些客戶端不能正確顯示其電子郵件的結尾MIME邊界。

的最後一行之前mail();

$message .= "--{$uid}--\n"; // terminate mime boundary 

編輯補充一點:後您的意見,我注意到另一個問題。

$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\n\n"; 
$uid = md5(uniqid(time())); 

需要改爲:

$uid = md5(uniqid(time())); 
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\n\n"; 

邊界原本是空這或許可以解釋爲什麼有些郵件客戶端沒有顯示正確。

+0

Hmmn,我按照你的建議添加了結尾並重新執行了cron。所以現在我得到了Mac Mail中的原始html。但是當我在webmail中檢查客戶的Horde帳戶時,消息顯示爲「此消息部分包含HTML數據,但內聯HTML顯示被禁用。」 在新窗口中查看HTML數據。點擊視圖鏈接確實顯示正確顯示消息。所以現在桌子轉了。不知道它將如何出現在Outlook中。我在我的Mac郵件程序中的FWIW我看到這個:Mime-Version:1.0 Content-Type:multipart/mixed; boundary =「」 –

+0

看起來像額外的編輯做的伎倆。我只用2個客戶端進行了測試,他們都能夠讀取消息。它也在Mac Mail中正確地再次出現。我會嘗試與更多的客戶,如果一切正常,我會標記解決方案是正確的。感謝您的幫助drew010。 –

相關問題