gpt4 book ai didi

php - 忽略多部分/混合内容类型

转载 作者:搜寻专家 更新时间:2023-10-31 21:09:19 25 4
gpt4 key购买 nike

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

我正在使用 php 邮件功能。

下面的代码是从 gmail 的“Show Original”中复制的,除了标题之外,这正是查看电子邮件时所看到的,就像“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--

最佳答案

它看起来很好,当我尝试重现问题时它确实工作正常(接近尾声的示例)。您需要密切注意确切的 MIME 语法,以便在标题和定界符中生成正确的行结尾,如下面的语法所述。

电子邮件语法

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

multipart-body := [preamble CRLF]                  dash-boundary CRLF                  body-part *encapsulation                  close-delimiter                  [CRLF epilogue]dash-boundary := "--" boundarybody-part := MIME-part-headers [CRLF *OCTET]encapsulation := delimiter                 CRLF body-partdelimiter := CRLF dash-boundaryclose-delimiter := delimiter "--"

The code

<?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";

引用资料

关于php - 忽略多部分/混合内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24787908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com