在JavaScript中调用Gmail API以发送带有HTML正文和约100KB PDF附件的邮件时,附件会正确显示为附加到发件人的Gmail Sent文件夹中的邮件,但不会显示在收件人的邮件上。
API调用是对以下对象的POST
请求:
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 Uploading Attachments文档指出,当上传"附件"时需要CCD_ 2;简单的";附件(5MB以下)。我这样做是为了让我的请求包括Content-Length
,其中包含PDF附件总字节数的整数值。但是,我注意到Content-Length
没有包含在有效载荷中。
我尝试将邮件的Content-Type
从multipart/alternative
更改为multipart/mixed
,这样PDF附件就可以正确地附加到收件人的邮件上,但邮件的HTML正文呈现为纯文本(显示了HTML标记),还有一个名为noname.html
的附加附件,其中包括呈现为HTML的HTML内容。
我需要这样做,以便收件人邮件中的电子邮件既有HTML呈现的正文,也有PDF附件。
更新:我在这里上传了原始电子邮件的示例。发送的消息在左侧,接收的消息在右侧。
只需替换:
Content-Type: multipart/alternative; boundary=__boundary__
用于:
Content-Type: multipart/mixed; boundary=__boundary__
这是我用JS 编写的全部功能
函数createMimeMessage_(msg){
var nl="\n";var border=">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,
];
对于(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.prush("--"+边界+"--")//console.log(mimeBody);
return mimeBody.join(nl);
}
您的问题有两部分:
- 如何将附件传递给收件人
- 如何包含HTML的附件和纯文本替代方案
Tiger开发人员(multipart/alternative
至multipart/mixed
)部分回答了这一问题。正如您所指出的,问题在于,简单地这样做会使您无法使用替代纯文本。这是因为您正在删除multipart/alternative
,其特定角色是提供该替代方案。
你需要做的是创建第二个边界,然后将纯文本分组&HTML部分在一起。看看这个同样来自CTRLQ的例子,并注意到我包含的altBoundary
。
function createMimeMessage_(msg) {
var nl = "n";
var boundary = "__ctrlq_dot_org__";
var altBoundary = "__alt_ctrlq_dot_org__";
var mimeBody = [
"MIME-Version: 1.0",
"To: " + encode_(msg.to.name) + "<" + msg.to.email + ">",
"From: " + encode_(msg.from.name) + "<" + msg.from.email + ">",
"Subject: " + encode_(msg.subject), // takes care of accented characters
"Content-Type: multipart/mixed; boundary=" + boundary + nl,
"--" + boundary,
"Content-Type: multipart/alternative; boundary=" + altBoundary + nl,
"--" + altBoundary,
"Content-Type: text/plain; charset=UTF-8",
"Content-Transfer-Encoding: base64" + nl,
Utilities.base64Encode(msg.body.text, Utilities.Charset.UTF_8) + nl,
"--" + altBoundary,
"Content-Type: text/html; charset=UTF-8",
"Content-Transfer-Encoding: base64" + nl,
Utilities.base64Encode(msg.body.html, Utilities.Charset.UTF_8) + nl,
"--" + altBoundary + "--"
];
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 + "--");
return mimeBody.join(nl);
}