使用纯文本回退发送HTML时事通讯


Sending HTML newsletters with plain-text fallback

我目前正在使用一个脚本,该脚本使用file_get_contents获取php文件的内容,然后在电子邮件中将其发送给客户列表。我想修改脚本,允许纯文本回退,以降低被标记为垃圾邮件的风险。

这是我现在的脚本:

    function sendit($to,$subject,$body)
{
$headers    =   "To: <{$to}>'n".
                    "From: Test Newsletter Admin <newsletter@test.co.uk>'n".
                    "Reply-To: Test Newsletter Admin <newsletter@test.co.uk>'n".
                    "MIME-Version: 1.0'n".
                    "Content-Type: text/html; charset=ISO-8859-1'n";
    $mail_sent = @mail($to, $subject, $body, $headers);
    return $mail_sent;
}
$content = file_get_contents('attach/newsletter.php');
//require_once('../../func.php');
set_time_limit(0);
date_default_timezone_set('Europe/London');
$log = fopen('send'.date('dmY',time()).'.log','wb');
//$array = file('NonCustClean.txt');
$array = file('devaddresses.txt');
// Delay In Seconds between emails (must be INT)
$delay = 10;
$count = count($array);
$end = time()+ ($delay*$count);
echo "Start Time: ".date('d/m/Y H:i:s',time()).'<br />';
echo "Estimated End Time: ".date('d/m/Y H:i:s',$end).'<br />';
echo "(".dateDiff(time(),$end).")<br />"; 
foreach ($array as $email)
 {
$status = (sendit(trim($email), 'Test Newsletter',$content))?'Sent':'failed';
 fwrite($log,date('[d/m/Y H:i:s]',time()).' Email '.$status.' to '.trim($email)."'n");
 echo date('[d/m/Y H:i:s]',time()).' Email '.$status.' to '.trim($email)."</br>";
 flush(); 
  sleep(10);
 }

时事通讯.php只包含基本的HTML/CSS代码。

有人能告诉我如何修改这个脚本以配合纯文本回退吗?

谢谢你的帮助。

您要查找的内容被称为多部分电子邮件。它的主体包含HTML和文本,由所谓的"边界"分隔。然后,电子邮件客户端将根据其功能和用户偏好自行决定是否显示邮件的HTML或Text版本。

关于如何设置的示例(来源):

$notice_text = "This is a multi-part message in MIME format.";
$plain_text = "This is a plain text email.'r'nIt is very cool.";
$html_text = "<html><body>This is an <b style='color:purple'>HTML</b>" .
             "text email.'r'nIt is very cool.</body></html>";
$semi_rand = md5(time());
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);
$to = "Me <me@me.com>";
$bcc = "You <you@you.com>, Them <them@them.com>";
$from = "Me.com <me@me.com>";
$subject = "My Email";
$body = "$notice_text
--$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
$plain_text
--$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
$html_text
--$mime_boundary--";
if (@mail($to, $subject, $body, 
    "From: " . $from . "'n" . 
    "bcc: " . $bcc . "'n" . 
    "MIME-Version: 1.0'n" . 
    "Content-Type: multipart/alternative;'n" . 
    "     boundary=" . $mime_boundary_header))
    echo "Email sent successfully.";
else
    echo "Email NOT sent successfully!";

您应该修改

Content-Type: text/html; charset=ISO-8859-1'n

有关更多信息,请查看此网站