gpt4 book ai didi

email - 使用 Gmail API 发送的邮件中缺少附件,但仅适用于收件人

转载 作者:行者123 更新时间:2023-12-03 23:56:29 32 4
gpt4 key购买 nike

在 Javascript 中使用 Gmail API 发送带有 HTML 正文和 ~100KB PDF 附件的邮件时,附件正确显示在发件人的 Gmail 已发送文件夹中的邮件附件中,但不会出现在收件人的邮件中。

API 调用是 POST到:

https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media

发送到 API 的请求正文是:
{
"headers": {
"Authorization": "Bearer authToken-removedForThisPost"
},
"method": "POST",
"contentType": "message/rfc822",
"contentLength": 134044,
"payload": "exampleBelow",
"muteHttpExceptions": true
}

这是有效载荷的样子:
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

base64EncodedStringHere-removedForThisPost

--__boundary__
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: base64

base64EncodedStringHere-removedForThisPost

--__boundary__
Content-Type: application/pdf; name="File Name.pdf"
Content-Disposition: attachment; filename="File Name.pdf"
Content-Transfer-Encoding: base64

base64EncodedStringHere-removedForThisPost

--__boundary__--

注意: Gmail API Uploading Attachments documentation声明上传简单附件(小于 5MB)时 Content-Length是必须的。我这样做是为了让我的代码生成 PDF 附件总字节数的整数值。但是,我注意到 Content-Length不包含在有效载荷中。

我尝试更改 multipart/alternative消息的内容类型 multipart/mixed - 这使得 PDF 附件正确附加到收件人的邮件,但邮件的 HTML 正文呈现为纯文本(显示 HTML 标签),并且还有一个名为 noname.html 的附加附件,其中包含 HTML内容呈现为 HTML。

我需要这样做,以便收件人消息中的电子邮件同时具有 HTML 呈现的正文和 PDF 附件。

更新:我上传了原始电子邮件的示例 here . 已发送 消息在左侧, 收到 消息在右侧。

最佳答案

只需更换:
Content-Type: multipart/alternative; boundary=__boundary__
为了:
Content-Type: multipart/mixed; boundary=__boundary__
这是我用 JS 编写的完整函数

function createMimeMessage_(msg) {

var nl = "\n"; var boundary = "ctrlq_dot_org";

var mimeBody = [

"MIME-Version: 1.0",
"To: " + msg.to.email,//+ encode_(msg.to.name) + "<" + msg.to.email + ">",
"Cc: " + msg.cc.email,
"Bcc: " + msg.bcc.email,
"From: " + msg.from.email,//+ encode_(msg.from.name) + "<" + msg.from.email + ">",
"Subject: " + encode_(msg.subject), // takes care of accented characters
"In-Reply-To: " + (msg.reply_to || ""),
"References: " + (msg.reply_to || ""),

"Content-Type: multipart/mixed; boundary=" + boundary + nl,
"--" + boundary,

// "Content-Type: text/plain; charset=UTF-8",
// "Content-Transfer-Encoding: 7bit",
// "Content-Disposition: inline" + nl,
// msg.body.text + nl,
// "--" + boundary,

"Content-Type: text/html; charset=UTF-8",
"Content-Transfer-Encoding: base64" + nl,
new Buffer(msg.body.text).toString('base64') + nl,

];

for (var i = 0; i < msg.files.length; i++) {

var attachment = [
"--" + boundary,
"Content-Type: " + msg.files[i].mimeType + '; name="' + msg.files[i].fileName + '"',
'Content-Disposition: attachment; filename="' + msg.files[i].fileName + '"',
"Content-Transfer-Encoding: base64" + nl,
msg.files[i].bytes
];

mimeBody.push(attachment.join(nl));

}

mimeBody.push("--" + boundary + "--"); //console.log(mimeBody);

return mimeBody.join(nl);

}

关于email - 使用 Gmail API 发送的邮件中缺少附件,但仅适用于收件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42054731/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com