使用纯文字后备广告发送HTML简报 [英] Sending HTML newsletters with plain-text fallback

查看:101
本文介绍了使用纯文字后备广告发送HTML简报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我目前的脚本: / p>

  function sendit($ to,$ subject,$ body)
{
$ headers =To: < {$ to}> \\\

From:Test Newsletter Admin< newsletter@test.co.uk> \\\

Reply-To:Test Newsletter Admin< newsletter@test.co.uk> \\\

MIME-Version:1.0 \\\

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

$ 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');
//延迟电子邮件之间的秒数(必须是INT)
$ delay = 10;
$ count = count($ array);

$ end = time()+($ delay * $ count);

echo开始时间:.date('d / m / Y H:i:s',time())''< br />
echoEstimated End Time:.date('d / m / Y H:i:s',$ end)。'< br />
echo(.dateDiff(time(),$ end)。)< br />;

foreach($ array as $ email)
{

$ status =(sendit($ email),'Test Newsletter',$ content) ?'发送':'失败';
fwrite($ log,date('[d / m / YH:i:s]',time())'Email'。$ status。'to'.trim($ email) n);
echo date('[d / m / YH:i:s]',time())。'Email'。$ status。'to'.trim($ email)。< / br> ;
flush();
sleep(10);
}

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

$



感谢您的帮助。

p>

解决方案

您正在寻找的是多部分电子邮件。它的body包含HTML和文本,由所谓的边界分隔。然后,电子邮件客户端将根据其功能和用户偏好来确定它是否将显示邮件的HTML或文本版本。



可以进行设置():

  $ notice_text =这是一个MIME格式的多部分消息。 
$ plain_text =这是一封纯文本电子邮件。\r\\\
这是很酷的。
$ html_text =< html>< body>这是< b style ='color:purple'> HTML< / b> 。
text email.\r\\\
It is cool。< / body>< / html>;

$ sem_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 =我的电子邮件;

$ 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。\\\

bcc: bcc。\\\

MIME-Version:1.0 \\\

Content-Type:multipart / alternative; \\\

boundary = $ mime_boundary_header))
echo电子邮件已成功发送。
else
echo电子邮件未成功发送!


I am currently using a script which uses file_get_contents to get the contents of a php file, and then sends it inside an email to a list of customers. I would like to alter the script to allow for plain-text fallback to reduce the risk of being marked as spam.

This is the script I have at the moment:

    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 just contains basic HTML/CSS code.

Could someone please advise how I can alter this script to accompany a plain-text fallback?

Thanks for any help.

解决方案

What you're looking for is known as a multipart e-mail. It's body contains both HTML and text, delimited by a so called "boundary". The e-mail client will then determine itself if it will show the HTML or Text version of the mail, based on it's capabilities and user preferences.

An example on how you can set this up (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!";

这篇关于使用纯文字后备广告发送HTML简报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆