【问题标题】:Sending HTML newsletters with plain-text fallback发送带有纯文本后备的 HTML 新闻通讯
【发布时间】:2012-02-06 02:49:04
【问题描述】:

我目前正在使用一个脚本,该脚本使用 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 代码。

有人可以告诉我如何更改此脚本以配合纯文本后备吗?

感谢您的帮助。

【问题讨论】:

  • 你有没有想过使用像Swiftmailer这样的库?这个answer 描述了发送 HTML 和纯文本邮件是多么容易。

标签: php html css email newsletter


【解决方案1】:

您要查找的内容称为多部分电子邮件。它的主体包含 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!";

【讨论】:

  • 我试过这个,电子邮件最终进入了垃圾邮件文件夹。 mail-tester.comMessage is 10% to 20% HTML obfuscation。此外,它说我不允许从指定的电子邮件地址发送邮件。
【解决方案2】:

你应该修改

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

更多信息,请查看site

【讨论】:

    猜你喜欢
    • 2015-05-06
    • 2013-06-10
    • 2016-03-13
    • 1970-01-01
    • 2020-04-06
    • 2011-04-21
    • 2021-09-01
    • 2013-04-07
    • 1970-01-01
    相关资源
    最近更新 更多