Emp*_*yee 13 email gmail attachment email-attachments gmail-api
在Javascript中使用Gmail API发送带有HTML正文和~100KB PDF附件的邮件时,附件会正确显示在发件人的Gmail已发送文件夹中的邮件中,但不会显示在收件人的邮件中.
API调用是POST:
https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media
Run Code Online (Sandbox Code Playgroud)
发送给API的请求正文是:
{
"headers": {
"Authorization": "Bearer authToken-removedForThisPost"
},
"method": "POST",
"contentType": "message/rfc822",
"contentLength": 134044,
"payload": "exampleBelow",
"muteHttpExceptions": true
}
Run Code Online (Sandbox Code Playgroud)
这是有效载荷的样子:
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__--
Run Code Online (Sandbox Code Playgroud)
注意:Gmail API上传附件文档指出,上传简单附件(5MB以下)时Content-Length需要.我做了它,以便我的代码生成PDF附件的总字节数的整数值.但是,我注意到它Content-Length没有包含在有效载荷中.
我尝试将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边界 = " ctrlq_dot_org ";
var mimeBody = [
Run Code Online (Sandbox Code Playgroud)"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++) {
Run Code Online (Sandbox Code Playgroud)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("--" + 边界 + "--"); //console.log(mimeBody);
返回 mimeBody.join(nl);
}
| 归档时间: |
|
| 查看次数: |
1582 次 |
| 最近记录: |