Gmail API:在一封邮件中发送文本和HTML [英] Gmail API: send text and HTML in one mail
问题描述
发送文本或HTML的邮件都可以很好地工作,但是当我在单个邮件中同时发送两者时,HTML都带有一个名为"noname.html"的附件,其中包含HTML.
Mail sending either text or HTML works perfectly fine, but when I send them both in single mail, HTML comes as an attachment named "noname.html", which contains HTML.
我已经阅读了与此主题相关的其他问题,但找不到可能出问题的地方.
I have read other related questions on this topic, but couldn't find what is possibly wrong.
MIME-Version: 1.0
From: sender@gmail.com
To: receiver@gmail.com
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正文和文本正文.如果我的理解是正确的,那么该修改如何?
You want to send both HTML body and text body using gmail API. If my understanding is correct, how about this modification?
Content-type: multipart/mixed; boundary="012boundary"
收件人:
Content-type: multipart/alternative; boundary="012boundary"
注意:
- 使用
multipart/alternative
,可以同时发送Content-type: text/plain; charset="UTF-8"
和Content-type: text/html; charset="UTF-8"
的两个部分. - Using
multipart/alternative
, both parts ofContent-type: text/plain; charset="UTF-8"
andContent-type: text/html; charset="UTF-8"
can be sent. - 多份/混合
- 多部分/替代
- 文本/纯文本
- text/html
- multipart/mixed
- multipart/alternative
- text/plain
- text/html
MIME-Version: 1.0 From: sender@gmail.com To: receiver@gmail.com 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--
参考文献:
- https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
- http://qcode.co.uk/post/70
- https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
- http://qcode.co.uk/post/70
References :
这篇关于Gmail API:在一封邮件中发送文本和HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- multipart/alternative
- 多部分/替代
Note :
在我的环境中,我可以确认已将您的请求正文修改为multipart/alternative
.如果这在您的环境中不起作用,对不起.
In my environment, I could confirm that your request body which was modified to multipart/alternative
worked. If this didn't work on your environment, I'm sorry.
为了将文本正文,HTML正文和HTML的附件文件作为一封电子邮件发送,可以如下创建请求正文的结构.
In order to send a text body, a HTML body and an attachment file of HTML as one email, the structure of request body can be created as follows.