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

标签 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');

我想在一封电子邮件中发送两个版本的电子邮件。一份文本和一份 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();

但似乎效果不佳..我想知道为什么!?!?

最佳答案

为了创建一个multipart/alternative 消息,您需要指定一个边界 并用该边界分隔每个部分。一个好的边界字符串应该是消息部分本身不太可能出现的东西,例如 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 中指定.

关于php - 使用 php 纯文本和 html 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10267840/

相关文章:

php - 如何仅使用我的更改来更新我的 symfony 模块?

php - 如果出现多行,如何在 php mysql 的侧存储过程中执行选择查询?

c# - 使用 c# .net 库检查来自 gmail 服务器的 IMAP 邮件

java - 如何将邮件中的base64图像附加到JavaMail和MimeMessageHelper?

php - iOS 到 PHP 的错误编码(非英语变成乱码)

php - 如何从两个有条件的表中进行选择

python - 将电子邮件发送到我的 python/flask 应用程序代码中的第一步是什么?

linux - 在 Linux 上通过 Qt 发送邮件通知

javascript - 将文本区域中的内容上传到文件夹

javax.mail.MessagingException : Could not connect to SMTP host: email-smtp. us-east-1.amazonaws.com,端口:25;