2012-04-22 70 views
4

一封電子郵件,我有以下電子郵件標題:發送與PHP的純文本和HTML

$random_hash = md5(date('r', time())); 
    $headers = array ('From' => $from, 
    'To' => $to, 
    'Return-Path' => '[email protected]', 
    'Subject' => $subject, 
    'Content-Type'=>'multipart/alternative'); 

我想發送電子郵件的兩個版本中的一個電子郵件。一個文本和一個HTML。

所以我這樣做:

ob_start(); ?> 
--PHP-alt-<?php echo $random_hash; ?> 
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit 
    Copy and paste: http://example.com/app_dl.php?app_id=<?php echo $_GET[app_id]; ?> to download your $app_name app \r\n 
    Want to get tons more hot apps for free! anywhere anytime? Download our app on http://example.com \r\n 
    example.com team; 

--PHP-alt-<?php echo $random_hash; ?> 
    Content-Type: text/html; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit 
    <p><a href='http://example.com/app_dl.php?app_id=<?php echo $_GET[app_id]; ?>'>Click Here</a> to download your <?php echo $app_name_app; ?></p> 
    <p>Want to get tons more hot apps for free! anywhere anytime? Download our app on <a href='http://example.com'>example.com</a></p> 
    <br/> 
    <p>example.com team</p>"; 


    <?php 
     $bodyHTML =ob_get_clean(); 

但它似乎沒有工作well..and我不知道爲什麼!?!?

+0

嗯爲什麼你手動創建所有的MIME並且不使用類來爲它指定HTML和文本內容? – ThiefMaster 2012-04-22 12:16:00

+0

1.定義「不好用」。 2.「Content-Type」和「Content-Transfer-Encoding」應該是標題的一部分。 – SimSimY 2012-04-22 12:16:50

+0

@TheSimon,他們是標題的一部分,問題是,在多部分消息中的部分是不分開的... – rid 2012-04-22 12:17:50

回答

15

爲了創建一個multipart/alternative消息,你需要指定一個boundary,並通過邊界各部分分開。一個好的邊界字符串是不太可能發生在消息部分本身的,例如sha1(uniqid())生成的隨機字符串。例如:

MIME-Version: 1.0 
Content-Type: multipart/alternative; boundary=c4d5d00c4725d9ed0b3c8b 

--c4d5d00c4725d9ed0b3c8b 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

part1 

--c4d5d00c4725d9ed0b3c8b 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

<b>part2</b> 

--c4d5d00c4725d9ed0b3c8b-- 

這是在RFC 2046, section 5.1.4中指定的。

+0

damit..doesnt work..see update – 2012-04-22 12:27:14

+0

有關這方面的工作示例,請參閱文章「使用附件發送電子郵件」一節「[PHP:發送電子郵件(文本/ HTML /附件)](http://webcheatsheet.com/PHP/send_email_text_html_attachment.php)」。 – creemama 2012-04-22 12:28:07

+0

那就是我現在正在閱讀的文章..查看更新 – 2012-04-22 12:29:29

-1

我發現使用PHP_EOL而不是\ r \ n似乎也能工作。例如:

$headers = "Content-type: text/html; charset=UTF8" . PHP_EOL; 
+0

PHP_EOL基於您使用的平臺。因此,如果你採用與在Linux上運行相同的腳本並將它移動到Windows盒子,它將開始發送「\ n」而不是「\ r \ n」,這可能不是你想要的。 – 2016-12-14 20:02:39

相關問題