发送文本或 HTML 的邮件工作得很好,但是当我在一封邮件中两者发送它们时,HTML 以名为“noname.html”的附件形式出现,其中包含 HTML。
我已阅读有关此主题的其他相关问题,但找不到可能存在的问题。
MIME-Version: 1.0
From: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3043555e54554270575d51595c1e535f5d" rel="noreferrer noopener nofollow">[email protected]</a>
To: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="24564147414d524156644349454d480a474b49" rel="noreferrer noopener nofollow">[email protected]</a>
Subject: test
Content-type: multipart/mixed; boundary="012boundary"
--012boundary
Content-type: text/plain; charset="UTF-8"
Hello plain text!
--012boundary
Content-type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<b>Hello html</b>
--012boundary--`
最佳答案
您想要使用 gmail API 发送 HTML 正文和文本正文。如果我的理解是正确的,那么这个修改怎么样?
来自:
Content-type: multipart/mixed; boundary="012boundary"
致:
Content-type: multipart/alternative; boundary="012boundary"
注意:
- 使用
multipart/alternative
,内容类型的两个部分:text/plain; charset="UTF-8"
和Content-type: text/html; charset="UTF-8"
可以发送。
引用:
在我的环境中,我可以确认您修改为 multipart/alternative
的请求正文有效。如果这不适用于您的环境,我很抱歉。
编辑:
为了将文本正文、HTML 正文和 HTML 附件文件作为一封电子邮件发送,可以按如下方式创建请求正文的结构。
- 多部分/混合
- 多部分/替代
- 文本/纯文本
- 文本/html
- text/html(附件文件)
- 多部分/替代
示例请求正文:
MIME-Version: 1.0
From: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8bbada6acadba88afa5a9a1a4e6aba7a5" rel="noreferrer noopener nofollow">[email protected]</a>
To: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="63110600060a15061123040e020a0f4d000c0e" rel="noreferrer noopener nofollow">[email protected]</a>
Subject: test
Content-Type: multipart/mixed; boundary=012boundary01
--012boundary01
Content-Type: multipart/alternative; boundary=012boundary02
--012boundary02
Content-type: text/plain; charset=UTF-8
Hello plain text!
--012boundary02
Content-type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<b>Hello html</b>
--012boundary02--
--012boundary01
Content-type: text/html; charset=UTF-8
Content-Disposition: attachment; filename="sample.html"
Content-Transfer-Encoding: quoted-printable
<b>HTML sample attachment file</b>
--012boundary01--
引用文献:
关于Gmail API : send text and HTML in one mail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50540051/