使用php纯文本和html发送电子邮件

Dmi*_*iyd 8 php email

我有以下电子邮件标题:

$random_hash = md5(date('r', time()));
    $headers = array ('From' => $from,
   'To' => $to,
    'Return-Path' => 'info@example.com',
   'Subject' => $subject,
     'Content-Type'=>'multipart/alternative');
Run Code Online (Sandbox Code Playgroud)

我想在一封电子邮件中发送两个版本的电子邮件.一个文字和一个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();
Run Code Online (Sandbox Code Playgroud)

但它似乎没有运作良好..我想知道为什么!?!?

rid*_*rid 18

要创建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--
Run Code Online (Sandbox Code Playgroud)

这在RFC 2046的5.1.4节中规定.