Php 电子邮件脚本格式不正确
我有一个脚本,它通过cron作业每月运行一次,向客户端发送awstats报告。我们最近不得不修改它,因为我们的出站邮件服务器限制越来越严格。不幸的是,有些收件人在邮件正文中以原始html代码的形式接收报告。有人能从剧本中看出我哪里出错了吗Php 电子邮件脚本格式不正确,php,curl,html-email,awstats,Php,Curl,Html Email,Awstats,我有一个脚本,它通过cron作业每月运行一次,向客户端发送awstats报告。我们最近不得不修改它,因为我们的出站邮件服务器限制越来越严格。不幸的是,有些收件人在邮件正文中以原始html代码的形式接收报告。有人能从剧本中看出我哪里出错了吗 ########################## ## Call to get awstats ########################## $curl = curl_init(); curl_setopt($curl, CURL
##########################
## 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);
#########################
##打电话获取awstats
##########################
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,”http://www.$your_domain:2082/awstats.pl“
.“?月=$report\u月&年=$report\u年&output=main&config=$your\u域”
“&lang=en&framename=mainright”);
curl_setopt($curl,CURLOPT_USERPWD,“$user:$password”);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
//$attachment=curl\u exec($curl);
$attachment=“
\n”;
$attachment.=“
针对$your\u域的AwStats报告($Report\u monthname$Report\u year)\n”;
$attachment.=“\n”;
$attachment.=curl\u exec($curl);
curl_close($curl);
##########################
##打电话发送电子邮件
##########################
$subject=“您的$u域的AwStats报告($Report\u monthname$Report\u year)”;
$header=“From:”$给你发电子邮件。“\n”;
$header.=“回复:”$给你发电子邮件。“\n”;
$header.=“MIME版本:1.0\n”;
$header.=“内容类型:多部分/混合;边界=\”.$uid.\“\n\n”;
$uid=md5(uniqid(time());
$message=“--”$uid。“\n”;
$message.=“内容类型:text/html;字符集=iso-8859-1\n”;
$message.=“内容传输编码:7bit\n\n”;
$message.=$attachment。“\n\n”;
邮件($email_to,$subject,$message,$header);
我显然忽略了这里的变量声明。但它们都在代码中。我实际上收到了一封抄送邮件,它在桌面上的Apple Mail中显示得很好
谢谢,
CJ电子邮件中缺少结尾MIME边界,这将导致某些客户端无法正确显示该边界 将此添加为
mail()之前的最后一行代码>
编辑:在你的评论之后,我注意到了另一个问题
$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";
边界最初是空的,这可能解释了为什么某些邮件客户端没有正确显示边界。客户端可能正在使用outlook。我使用的是Mac Mail.Hmmn,我按照您的建议添加了结束,并重新运行了cron。现在我在Mac Mail中获得原始html。但当我在webmail中签入客户的Horde帐户时,消息显示为“此消息部分包含HTML数据,但内联HTML显示被禁用。在新窗口中查看HTML数据。”单击查看链接确实可以正确显示消息。因此,现在形势发生了转变。不确定它将如何显示在Outlook中。FWIW在我的Mac Mail程序的标题中我看到:Mime版本:1.0内容类型:多部分/混合;boundary=“”看起来是额外的编辑完成的。我刚刚测试了两个客户,他们都能阅读信息。它也在Mac Mail中再次正确地传递过来。我会尝试更多的客户,如果一切顺利,我会标记为正确的解决方案。谢谢你的帮助。
$uid = md5(uniqid(time()));
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\n\n";