php – 使用纯文本回退发送HTML简报
作者:互联网
我目前正在使用一个脚本,该脚本使用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代码.
有人可以建议我如何改变这个脚本以伴随纯文本回退?
谢谢你的帮助.
解决方法:
您正在寻找的是多部分电子邮件.它的主体包含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!";
标签:php,css,email,html,newsletter 来源: https://codeday.me/bug/20190521/1146827.html