【问题标题】:Plaintext/HTML Email doesn't work in mac mail client纯文本/HTML 电子邮件在 mac 邮件客户端中不起作用
【发布时间】:2010-09-25 19:53:45
【问题描述】:

下面的代码是我正在使用的代码。它在雷鸟中运行良好,但在 mac 邮件客户端中运行良好(我假设任何东西都是由微软制作的。我目前无法访问它来测试它)。尽管我知道各种邮件客户端的特性,但我对此感到困惑!这是相当不言自明的,但我正在尝试发送纯文本和 html 电子邮件以增加读者群。任何帮助将不胜感激。

编辑

我应该澄清一下,无论如何都会发送内容,但在 Thunderbird 中它会正确显示消息,但在 mac 邮件客户端中,你会得到从第一个 PHP-alt 到最后一个 PHP 的全部内容

<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

【问题讨论】:

    标签: php email


    【解决方案1】:

    与其尝试滚动您自己的邮件,不如尝试例如PHPMailer。它对 multipart/alternative 有很好的支持。集成它比推出自己的解决方案要容易得多。我去过那里 - 在无休止地解决奇怪的 MIME 问题后,我放弃了我的手工邮件,切换到这个,并在我空闲的时间专注于其他事情。

    换句话说,不要重新发明轮子。虽然自己动手可能是一个很好的挑战,并且在此过程中您会学到很多东西,但如果您只是想让它发挥作用,这些人已经为您处理了复杂性。

    【讨论】:

      【解决方案2】:

      您没有正确使用输出缓冲 - 请参阅 ob_end_clean 的手册页以查看它没有返回捕获的输出,您需要 ob_get_contents 来解决此问题:

      $message =ob_get_contents();
      ob_end_clean();
      

      【讨论】:

      • 为此干杯,但可惜,我刚刚试了一下,它仍然不起作用。
      • 有东西送达吗?如果是这样,请发布电子邮件的完整来源
      猜你喜欢
      • 2013-02-07
      • 1970-01-01
      • 2015-09-02
      • 2015-09-25
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      相关资源
      最近更新 更多