当使用JavaScript调用Gmail API发送带有HTML正文和约100KB的PDF附件的邮件时,该附件会正确地附加到发件人的Gmail已发送文件夹中的邮件中,但在收件人的邮件中不会显示。API调用是向以下地址进行POST请求:
注意:Gmail API上传附件文档指出,在上传“简单”附件(小于5MB)时需要
我尝试将消息的
我需要确保收件人的邮件既有HTML渲染的正文,又有PDF附件。
更新:我已经上传了邮件的原始示例这里。左边是发送的消息,右边是接收的消息。
https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media
发送到API的请求类似于这样:
{
"headers": {
"Authorization": "Bearer {auth_token}"
},
"method": "POST",
"contentType": "message/rfc822",
"contentLength": 134044,
"payload": "{see_below}",
}
请求正文为:
MIME-Version: 1.0
To: =?utf-8?B?TWlrZSBD?=<recipient@test.com>
CC: =?utf-8?B?TWlrZSBD?=<secondrecipient@gmail.com>
BCC: =?utf-8?B??=<bccrecipient@test.com>
From: =?utf-8?B?TWlrZSBxWXsd2lr?=<sender@test.com>
Subject: =?utf-8?B?subjectLine-removedForThisPost?=
Content-Type: multipart/alternative; boundary=__boundary__
--__boundary__
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64
{base64_encoded_string}
--__boundary__
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: base64
{base64_encoded_string}
--__boundary__
Content-Type: application/pdf; name="File Name.pdf"
Content-Disposition: attachment; filename="File Name.pdf"
Content-Transfer-Encoding: base64
{base64_encoded_string}
--__boundary__--
注意:Gmail API上传附件文档指出,在上传“简单”附件(小于5MB)时需要
Content-Length
。我使请求包括Content-Length
,其值为PDF附件的总字节数。然而,我发现负载中没有包含Content-Length
。我尝试将消息的
Content-Type
从multipart/alternative
更改为multipart/mixed
- 这样做会使PDF附件正确附加到收件人的消息中,但消息的HTML正文被呈现为纯文本(显示HTML标签),并且还有一个名为noname.html
的附件,其中包含以HTML格式呈现的HTML内容。我需要确保收件人的邮件既有HTML渲染的正文,又有PDF附件。
更新:我已经上传了邮件的原始示例这里。左边是发送的消息,右边是接收的消息。
boundary="__boundary__"
)并使用最终边界(--__boundary__--
)吗?尝试类似于这样的东西,看看是否有效。 - Tholle--__boundary__--
的最终边界,但当我将其粘贴在这里时,它被截断了,因为控制台日志消息由于超长的附件 base64 字符串而被截断。至于双引号 - 我将它们添加到第一个Content-Type:
行中,但它并没有改变行为 - 它与或没有它一样工作。 - Employee