使用纯文本回退发送HTML简报

时间:2012-01-09 15:16:28

标签: php html css email newsletter

我目前正在使用一个脚本,该脚本使用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);
 }

Newsletter.php只包含基本的HTML / CSS代码。

有人可以建议我如何改变这个脚本以配合明文后备?

感谢您的帮助。

2 个答案:

答案 0 :(得分:5)

您正在寻找的内容称为多部分电子邮件。它的主体包含HTML和文本,由所谓的“边界”分隔。然后,电子邮件客户端将根据其功能和用户首选项确定是否显示邮件的HTML或文本版本。

关于如何设置它的示例(source):

$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!";

答案 1 :(得分:0)

你应该修改

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

有关更多信息,请查看此site