2012-01-09 32 views
5

我目前使用的腳本使用file_get_contents來獲取php文件的內容,然後將其發送到一個電子郵件內的客戶列表。我想修改腳本以允許純文本回退以降低被標記爲垃圾郵件的風險。發送純文本回退的HTML新聞簡報

這是劇本我都在時刻:

function sendit($to,$subject,$body) 
{ 
$headers = "To: <{$to}>\n". 
        "From: Test Newsletter Admin <[email protected]>\n". 
        "Reply-To: Test Newsletter Admin <[email protected]>\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代碼。

有人可以請告訴我如何改變這個腳本來陪伴純文本回退?

感謝您的任何幫助。

+0

你有沒有想過使用像[Swiftmailer]庫(http://swiftmailer.org/)?這[回覆](http://stackoverflow.com/a/29345590/987864)描述了發送HTML和純文本郵件是多麼容易。 – Daan 2015-03-30 11:59:25

回答

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 <[email protected]>"; 
$bcc = "You <[email protected]>, Them <[email protected]>"; 
$from = "Me.com <[email protected]>"; 
$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!"; 
+0

我試過了,電子郵件在垃圾郵件文件夾中結束。 http://www.mail-tester.com/說'消息是10%到20%的HTML混淆'。此外,它表示我不允許從指定的電子郵件地址發送郵件。 – erdomester 2014-03-19 12:57:05

0

您應修改

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

欲瞭解更多相關信息,請查看本site

相關問題