2012-04-22 76 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

相关问题