【问题标题】:multipart/mixed content-type ignored忽略多部分/混合内容类型
【发布时间】:2014-09-07 09:38:59
【问题描述】:

我的代码以硬编码方式输出 mime 电子邮件,这非常简单。 我制作了一个新的 api 来处理任何上下文,据我所知,我复制并粘贴了相关的 mime 语法。然而 gmail 显示的是原始 mime。

我正在使用php邮件功能。

以下代码是从 gmail 的“显示原始文件”复制而来,除了标题之外,这与查看电子邮件时所看到的完全相同,就好像“Content-Type:multipart/mixed...”标题被视为文本一样/plain...我做错了什么?

Return-Path: <blah@blah.com>
Received: from www.blah.com (www.blah.com. [xxx.xx.xxx.xx])
        by mx.google.com with ESMTPSA id c8sm28269395qam.21.2014.07.15.16.54.29
        for <blah@blah.com>
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Tue, 15 Jul 2014 16:54:29 -0700 (PDT)
Received: by www.blah.com (Postfix, from userid 27)
    id 7BF8B1CF2; Tue, 15 Jul 2014 19:54:28 -0400 (EDT)
To: blah@blah.com
Subject: blah blah blah
X-PHP-Originating-Script: 0:Blah.php
From: blah <blah@blah.com>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="--=_NextPart_BIG_UUID_1"
Message-Id: <20140715225428.7BF8B1CF2@www.blah.com>
Date: Tue, 15 Jul 2014 18:54:28 -0400 (EDT)

----=_NextPart_BIG_UUID_1  
content-type: multipart/alternative; boundary="--=_NextAltPart_BIG_UUID_2"

----=_NextAltPart_BIG_UUID_2
content-type: text/plain; charset=iso-8859-1
content-transfer-encoding: quoted-printable

test text content 

----=_NextAltPart_BIG_UUID_2
content-type: multipart/related; boundary="--=_NextAltRelPart_BIG_UUID_3"

----=_NextAltRelPart_BIG_UUID_3
content-type: text/html; charset=UTF8
content-transfer-encoding: quoted-printable

<html><body><h1>BLAH</h1></body></html>

----=_NextAltRelPart_BIG_UUID_3
content-type: text/plain;
Content-transfer-encoding: base64
Content-ID: <whatever_doesnt_matter.txt>

dGVzdCBjb250ZW50


----=_NextAltRelPart_BIG_UUID_3--

----=_NextAltPart_BIG_UUID_2--

----=_NextPart_BIG_UUID_1 
Content-Type: application/pdf; name=blah.pdf 
ContentDisposition: attachment;
Content-Transfer-Encoding: base64

JVBERBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLA
HBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLA
HBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLA
HTUzMj5dPj4Kc3RhcnR4cmVmCjQ1ODkyNgolJUVPRgo=


----=_NextPart_BIG_UUID_1--

【问题讨论】:

    标签: php mime multipart


    【解决方案1】:

    看起来不错,当我尝试重新创建问题时它确实可以正常工作(接近结尾的示例)。您需要密切注意确切的 MIME 语法,以便在标题和分隔符中生成正确的行尾,如下面的语法所述。

    电子邮件语法

    这是根据 RFC 2046 的多部分消息正文结构的摘录。请特别注意 CRLF 的结构。 (BNF 语法,有些简化。)

    multipart-body := [序言 CRLF] 虚线边界CRLF 身体部分 *封装 闭合分隔符 [CRLF 结语] dash-boundary := "--" 边界 body-part := MIME-part-headers [CRLF *OCTET] 封装 := 分隔符 CRLF 身体部位 delimiter := CRLF 破折号边界 close-delimiter := 分隔符“--”

    代码

    <?php
    
    $headers  = "From: user@domain.example\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"--=_NextPart_BIG_UUID_1\"\r\n";
    
    $body  = "----=_NextPart_BIG_UUID_1\r\n";
    $body .= "content-type: multipart/alternative; boundary=\"--=_NextAltPart_BIG_UUID_2\"\r\n";
    $body .= "\r\n";
    
    $body .= "----=_NextAltPart_BIG_UUID_2\r\n";
    $body .= "content-type: text/plain; charset=iso-8859-1\r\n";
    $body .= "content-transfer-encoding: quoted-printable\r\n";
    $body .= "\r\n";
    $body .= "test text content\n";
    
    $body .= "\r\n----=_NextAltPart_BIG_UUID_2";
    $body .= "\r\n";
    $body .= "content-type: multipart/related; boundary=\"--=_NextAltRelPart_BIG_UUID_3\"\r\n";
    $body .= "\r\n";
    
    $body .= "----=_NextAltRelPart_BIG_UUID_3\r\n";
    $body .= "content-type: text/html; charset=UTF8\r\n";
    $body .= "content-transfer-encoding: quoted-printable\r\n";
    $body .= "\r\n";
    $body .= "<html><body><h1>BLAH</h1></body></html>\n";
    
    $body .= "\r\n----=_NextAltRelPart_BIG_UUID_3\r\n";
    $body .= "content-type: text/plain;\r\n";
    $body .= "Content-transfer-encoding: base64\r\n";
    $body .= "Content-ID: <whatever_doesnt_matter.txt>\r\n";
    $body .= "\r\n";
    $body .= "dGVzdCBjb250ZW50\r\n";
    
    $body .= "\r\n----=_NextAltRelPart_BIG_UUID_3--\r\n";
    
    $body .= "\r\n----=_NextAltPart_BIG_UUID_2--\r\n";
    
    $body .= "\r\n----=_NextPart_BIG_UUID_1\r\n";
    $body .= "Content-Type: application/pdf; name=blah.pdf\r\n";
    $body .= "ContentDisposition: attachment;\r\n";
    $body .= "Content-Transfer-Encoding: base64\r\n";
    $body .= "\r\n";
    $body .= "JVBERBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLA\n";
    $body .= "HBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLA\n";
    $body .= "HBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLA\n";
    $body .= "HTUzMj5dPj4Kc3RhcnR4cmVmCjQ1ODkyNgolJUVPRgo=\r\n";
    
    $body .= "\r\n----=_NextPart_BIG_UUID_1--";
    
    $to = 'user@domain.example';
    $subject = 'MIME Test';
    
    return mail ($to, $subject, $body, $headers);
    //echo "$headers\n$body\n";
    

    参考文献

    【讨论】:

    • 我希望我能投票一千次。出于某种原因,这是我在互联网上找到的用于编写多部分电子邮件的唯一完整且有效的示例。他们真的应该把例子放在那些无聊的 RFC 规范中。虽然第 17 行缺少分号(包括空行和 \n\n 是否打算为 \r\n。跨度>
    • @doABarrelRoll721:不错的收获!我检查了答案的原始来源,当我对字符串中的行尾进行最终重新格式化时,分号似乎丢失了。
    • @doABarrelRoll721:重读 RFC5322 后发现,“CR 和 LF 只能作为 CRLF 一起出现;它们不能在正文中独立出现。”,所以两次出现的\n\n 不正确。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 2019-07-22
    • 2012-01-19
    • 1970-01-01
    • 2019-07-21
    • 2016-01-03
    相关资源
    最近更新 更多