电子邮件脚本未正确格式化

时间:2016-03-03 19:13:30

标签: php curl html-email awstats

我有这个脚本,每月一次通过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

1 个答案:

答案 0 :(得分: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";

边界最初是空的,这可以解释为什么有些邮件客户端没有正确显示它。