使用 Gmail 创建电子邮件 API

Create email with Gmail API

我正在这里 API 测试 Gmail https://developers.google.com/gmail/api/reference/rest/v1/users.drafts/create 但是否可以创建包含发件人、主题、正文并加载 Gmail 模板并通过 curl 命令发送的完整电子邮件?

这是我尝试过的方法,但我收到一条空消息:

如何使用 Gmail 发送基于草稿的电子邮件 API

这是您可以采用的一种方法:

使用users.draft list

{
  "drafts": [
    {
      "id": "r-1561xxx",
      "message": {
        "id": "179891xxx",
        "threadId": "179891xxx"
      }
    }
  ],
  "resultSizeEstimate": 1
}

然后 users.draft get 与上一个请求中的 id 之一:

您可以通过使用参数 format=raw 调用 drafts.get 来检索草稿中包含的当前 MIME 消息。

(数据已清理)

{
  "id": "r-1561xxx",
  "message": {
    "id": "179891xxx",
    "threadId": "179891xxx",
    "labelIds": [
      "DRAFT"
    ],
    "snippet": "Name: Reason: Company:",
    "sizeEstimate": 635,
    "raw": "TUlNRS1WZXJzaW9uOiAxLjAKRGF0ZTogRnJpLCAxMyBBdWcgMjAyMSAxNjoxMjowOSArMDIwMApNZXNzYWdlLUlEOiA8Q0FGZHVrYm5yZHhReHh4eEBtYWlsLmdtYWlsLmNvbT4KU3ViamVjdDogZHJhZnQxCkZyb206IFNlbmRlciA8c2FtcGxlQGdtYWlsLmNvbT4KVG86IFJlY2VpdmVyIDxzYW1wbGVAZ21haWwuY29tPgpDb250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9IjAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIiCgotLTAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKCk5hbWU6CgpSZWFzb246CgpDb21wYW55OgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJVVEYtOCIKCjxkaXYgZGlyPSJsdHIiPk5hbWU6PGRpdj48YnI+PC9kaXY+PGRpdj5SZWFzb246PC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5Db21wYW55OjwvZGl2PjwvZGl2PgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyLS0=",
    "historyId": "67xxx",
    "internalDate": "1628xxx"
  }
}

raw 属性是消息所在的位置。它以 base64 编码。解码后看起来像这样:

MIME-Version: 1.0
Date: Fri, 13 Aug 2021 16:12:09 +0200
Message-ID: <CAFdukbnrdxQxxxx@mail.gmail.com>
Subject: draft1
From: Sender <sample@gmail.com>
To: Receiver <sample@gmail.com>
Content-Type: multipart/alternative; boundary="000000000000eb4b5805c9716fd2"

--000000000000eb4b5805c9716fd2
Content-Type: text/plain; charset="UTF-8"

Name:

Reason:

Company:

--000000000000eb4b5805c9716fd2
Content-Type: text/html; charset="UTF-8"

<div dir="ltr">Name:<div><br></div><div>Reason:</div><div><br></div><div>Company:</div></div>

--000000000000eb4b5805c9716fd2--

正在发送

guide 中说:

Create the email content in some convenient way and encode it as a base64url string. Create a new message resource and set its raw property to the base64url string you just created.

文字应符合rfc2822.

所以制作一个像这样的纯文本电子邮件(您可以省略一些字段,例如日期和消息 ID):

MIME-Version: 1.0
Subject: draft1
From: Sender <sample@gmail.com>
To: Receiver <sample@gmail.com>
Content-Type: multipart/alternative; boundary="000000000000eb4b5805c9716fd2"

--000000000000eb4b5805c9716fd2
Content-Type: text/plain; charset="UTF-8"

Name: TooNetCreation

Reason: TooNetCreation

Company: TooNetCreation

--000000000000eb4b5805c9716fd2
Content-Type: text/html; charset="UTF-8"

<div dir="ltr">Name: TooNetCreation<div><br></div><div>Reason: TooNetCreation</div><div><br></div><div>Company: TooNetCreation</div></div>

--000000000000eb4b5805c9716fd2--

编码为:

TUlNRS1WZXJzaW9uOiAxLjAKU3ViamVjdDogZHJhZnQxCkZyb206IFNlbmRlciA8c2FtcGxlQGdtYWlsLmNvbT4KVG86IFJlY2VpdmVyIDxzYW1wbGVAZ21haWwuY29tPgpDb250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9IjAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIiCgotLTAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKCk5hbWU6IFRvb05ldENyZWF0aW9uCgpSZWFzb246IFRvb05ldENyZWF0aW9uCgpDb21wYW55OiBUb29OZXRDcmVhdGlvbgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJVVEYtOCIKCjxkaXYgZGlyPSJsdHIiPk5hbWU6IFRvb05ldENyZWF0aW9uPGRpdj48YnI+PC9kaXY+PGRpdj5SZWFzb246IFRvb05ldENyZWF0aW9uPC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5Db21wYW55OiBUb29OZXRDcmVhdGlvbjwvZGl2PjwvZGl2PgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyLS0=

您可以发送:

curl --request POST \
  'https://gmail.googleapis.com/gmail/v1/users/me/messages/send?key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '{"raw":"TUlNRS1WZXJzaW9uOiAxLjAKU3ViamVjdDogZHJhZnQxCkZyb206IFNlbmRlciA8c2FtcGxlQGdtYWlsLmNvbT4KVG86IFJlY2VpdmVyIDxzYW1wbGVAZ21haWwuY29tPgpDb250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9IjAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIiCgotLTAwMDAwMDAwMDAwMGViNGI1ODA1Yzk3MTZmZDIKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKCk5hbWU6IFRvb05ldENyZWF0aW9uCgpSZWFzb246IFRvb05ldENyZWF0aW9uCgpDb21wYW55OiBUb29OZXRDcmVhdGlvbgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJVVEYtOCIKCjxkaXYgZGlyPSJsdHIiPk5hbWU6IFRvb05ldENyZWF0aW9uPGRpdj48YnI+PC9kaXY+PGRpdj5SZWFzb246IFRvb05ldENyZWF0aW9uPC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5Db21wYW55OiBUb29OZXRDcmVhdGlvbjwvZGl2PjwvZGl2PgoKLS0wMDAwMDAwMDAwMDBlYjRiNTgwNWM5NzE2ZmQyLS0="}' \
  --compressed

之后它应该 return 消息的实例,您将发送一封电子邮件!

参考资料