【问题标题】:Problems with sending a multipart/alternative email with PHP使用 PHP 发送多部分/替代电子邮件的问题
【发布时间】:2009-03-26 16:30:19
【问题描述】:

这是构建/发送电子邮件的脚本:

$boundary = md5(date('U'));

$to = $email;
$subject = "My Subject";

$headers = "From: myaddress@mydomain.com" . "\r\n".
           "X-Mailer: PHP/".phpversion() ."\r\n".
           "MIME-Version: 1.0" . "\r\n".
           "Content-Type: multipart/alternative; boundary=--$boundary". "\r\n".
           "Content-Transfer-Encoding: 7bit". "\r\n";

$text = "You really ought remember the birthdays";     
$html = '<html>
    <head>
      <title>Birthday Reminders for August</title>
    </head>
    <body>
      <p>Here are the birthdays upcoming in August!</p>
      <table>
        <tr>
          <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
        </tr>
        <tr>
          <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
        </tr>
        <tr>
          <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
        </tr>
      </table>
    </body>
    </html>
    ';

$message = "Multipart Message coming up" . "\r\n\r\n".
       "--".$boundary.
       "Content-Type: text/plain; charset=\"iso-8859-1\"" .
       "Content-Transfer-Encoding: 7bit".
       $text. 
       "--".$boundary. 
       "Content-Type: text/html; charset=\"iso-8859-1\"". 
       "Content-Transfer-Encoding: 7bit". 
       $html.
       "--".$boundary."--";



mail("toAddress@example.com", $subject, $message, $headers);

它发送的消息很好,我的收件人收到了,但他们以文本/纯文本而不是多部分/替代形式获得整个信息。查看收到的消息的来源给出了这个(删除了很​​多垃圾):

Delivered-To: myrecipient@example.com
Received: by 10.90.100.4 with SMTP id x4cs111413agb;
    Wed, 25 Mar 2009 16:39:32 -0700 (PDT)
Received: by 10.100.153.6 with SMTP id a6mr85081ane.123.1238024372342;
    Wed, 25 Mar 2009 16:39:32 -0700 (PDT)
Return-Path: <xxx@xxxx.com>
--- snip ---
Date: Wed, 25 Mar 2009 17:37:36 -0600 (MDT)
Message-Id: <200903252337.n2PNbaw2019541@www.xxxxxxx.com>
To: trevor@saturdayplace.com
Subject: My Subject
From: me@mydomain.com
X-Mailer: PHP/4.3.9
MIME-Version: 1.0
Content-Type: text/plain;
boundary="--66131caf569f63b24f43d529d8973560"
Content-Transfer-Encoding: 7bit
X-OriginalArrivalTime: 25 Mar 2009 23:38:30.0531 (UTC) FILETIME=[CDC4E530:01C9ADA2]
X-TM-AS-Product-Ver: SMEX-8.0.0.1181-5.600.1016-16540.005
X-TM-AS-Result: No--4.921300-8.000000-31
X-TM-AS-User-Approved-Sender: No
X-TM-AS-User-Blocked-Sender: No


Multipart Message coming up

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

You really ought remember the birthdays

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

<html>
    <head>
      <title>Birthday Reminders for August</title>
    </head>
    <body>
      <p>Here are the birthdays upcoming in August!</p>
      <table>
        <tr>
          <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
        </tr>
        <tr>
          <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
        </tr>
        <tr>
          <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
        </tr>
      </table>
    </body>
    </html>


--66131caf569f63b24f43d529d8973560--

看起来内容类型标题正在从多部分/替代到文本/纯文本的过程中发生变化。我不是系统管理员,所以如果这是一个 sendmail 问题,我会感到头疼。有什么建议吗?

【问题讨论】:

    标签: php email


    【解决方案1】:

    线

    "Content-Type: multipart/alternative; boundary=--$boundary". "\r\n".
    

    应该是

    "Content-Type: multipart/alternative; boundary=$boundary". "\r\n".
    

    您没有在标题中包含破折号。

    【讨论】:

    • 嘎啊啊啊! - 感谢罗博格。确实是这样。
    • 如果有人想知道为什么初始脚本不起作用 - 只需添加 . "\r\n" 到 $message 包含的每一行,而不仅仅是在第一行 ($message = "Multipart Messagecoming up" . "\r\n\r\n".) 啊,对,Sam Critchley已经指出了这一点。
    【解决方案2】:

    为了让它对我正常工作,我还需要在每个标题行的末尾添加一个“\r\n”,其中两个(“\r\n\r\n”)在末尾文本和 html 正文之前的行数,每个正文末尾的行数。结果是这样的:

    $message = "Multipart Message coming up" . "\r\n\r\n".
           "--".$boundary."\r\n".
           "Content-Type: text/plain; charset=\"iso-8859-1\""."\r\n".
           "Content-Transfer-Encoding: 7bit"."\r\n".
           $text."\r\n". 
           "--".$boundary."\r\n". 
           "Content-Type: text/html; charset=\"iso-8859-1\""."\r\n". 
           "Content-Transfer-Encoding: 7bit"."\r\n".
           $html."\r\n".
           "--".$boundary."--";
    

    【讨论】:

    • 这也是它对我有用的唯一方法。令人惊讶的是,网上有多少例子忽略了这一点。
    【解决方案3】:

    使用“\n”而不是“\r\n”看起来qmail有“\r”的问题

    【讨论】:

    • 同意,我通过文档知道它不正确,但我们经常向我们的客户发送大量邮件,神奇的是,如果我在我们的服务器上使用 \r,消息会损坏,但如果我 str_replace \r 为标题和正文中的空''(仅保留 \n)使电子邮件正常到达并在大多数客户端中正确显示/测试桌面客户端和网络邮件/
    【解决方案4】:

    只是一个想法,但使用 phpmailerZend_Mail 之类的东西可能更容易获得价值。

    【讨论】:

      猜你喜欢
      • 2018-02-17
      • 1970-01-01
      • 2014-09-27
      • 2012-12-27
      • 2013-05-30
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 2014-06-21
      相关资源
      最近更新 更多