我的代码以硬编码的方式输出了一个mime电子邮件,这很简单。 我创建了一个新的api来处理任何上下文,据我所知,我复制并粘贴了相关的mime语法。然而gmail显示原始的哑剧。
我正在使用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--
答案 0 :(得分:2)
看起来很好,当我尝试重新创建问题时(实例接近结束时)确实可以正常工作。您需要密切关注确切的MIME语法,以便在标题和分隔符中生成正确的行结尾,如下面的语法所述。
根据RFC 2046,这是多部分邮件正文结构的摘录。请特别注意CRLF
。 (BNF语法,有点简化。)
multipart-body := [preamble CRLF] dash-boundary CRLF body-part *encapsulation close-delimiter [CRLF epilogue] dash-boundary := "--" boundary body-part := MIME-part-headers [CRLF *OCTET] encapsulation := delimiter CRLF body-part delimiter := CRLF dash-boundary close-delimiter := 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";