Gmail API:在一封邮件中发送文本和HTML [英] Gmail API: send text and HTML in one mail

查看:118
本文介绍了Gmail API:在一封邮件中发送文本和HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发送文本或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"的两个部分.
  • Note :

    • Using multipart/alternative, both parts of Content-type: text/plain; charset="UTF-8" and Content-type: text/html; charset="UTF-8" can be sent.
    • 在我的环境中,我可以确认已将您的请求正文修改为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.

      • 多份/混合
        • 多部分/替代
          • 文本/纯文本
          • 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--
              

              参考文献:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆