电子邮件脚本格式不正确
email script not formatting properly
我有这个脚本,它每月通过 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
您在电子邮件中缺少结尾的 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";
边界最初是空的,这可能解释了为什么有些邮件客户端没有正确显示它。
我有这个脚本,它每月通过 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
您在电子邮件中缺少结尾的 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";
边界最初是空的,这可能解释了为什么有些邮件客户端没有正确显示它。