我已经使用“媒体上传”方法创建了消息草稿 使用下面的代码
var draftUploadUrl = "https://www.googleapis.com/upload/gmail/v1/users/me/drafts?uploadType=media";
var response = UrlFetchApp.fetch(draftUploadUrl, {
method: "POST",
headers: {
//authorizing request through service account
"Authorization": "Bearer " + service.getAccessToken(),
"Content-Type": "message/rfc822",
},
muteHttpExceptions: true,
//payload_data is mime content with base64 encode of email body an
//attachment
payload: payload_data
});
draftID = /: "(.*)"/.exec(response.getContentText())[1];
console.log("draftID: " + draftID);
我收到邮件的草稿ID,如何在参数中设置草稿ID 以发送带有附件的电子邮件?
代码段
var resp1 = UrlFetchApp.fetch("https://www.googleapis.com/upload/gmail/v1/users/me/drafts/send?uploadType=media", {
method: "POST",
headers: {
"Authorization": "Bearer " + service.getAccessToken(),
"Content-Type": "message/rfc822"
},
muteHttpExceptions: true,
payload: JSON.stringify({
"id": draftID
})
});
它抛出错误“无效草稿”。能否请您指导如何为上述url调用传递ID参数,或者上述代码出了什么问题?
谢谢。
答案 0 :(得分:0)
如果我的理解是正确的,那么该修改如何?
https://www.googleapis.com/gmail/v1/users/me/drafts/send
作为端点。application/json
用作Content-Type
。var resp1 = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/me/drafts/send", {
method: "POST",
headers: {
"Authorization": "Bearer " + service.getAccessToken(),
"Content-Type": "application/json"
},
muteHttpExceptions: true,
payload: JSON.stringify({
"id": draftID
})
});
如果我误解了你的问题,我深表歉意。
当您要创建包含附件文件的电子邮件草稿时,此示例脚本如何?来自https://stackoverflow.com/a/45992149/7108653。
此示例脚本使用Gmail API创建电子邮件草稿。
function createDraft() {
var fileId = "### file id ###"; // Please set this.
var file = DriveApp.getFileById(fileId);
var forScope = GmailApp.getInboxUnreadCount();
var htmlBody = 'sample HTML body'; // Please set this.
var raw =
'Subject: sample subject\r\n' + // Please set this.
'To: aa@bb.cc\r\n' + // Please set this.
'Content-Type: multipart/mixed; boundary=##########\r\n\r\n' +
'--##########\r\n' +
'Content-Type: text/html; charset=UTF-8\r\n\r\n' + htmlBody + '\r\n' +
'--##########\r\n' +
'Content-Type: ' + file.getMimeType() + '; charset=UTF-8; name="' + file.getName() + '"\r\n' +
'Content-Disposition: attachment; filename="' + file.getName() + '"\r\n' +
'Content-Transfer-Encoding: base64\r\n\r\n' + Utilities.base64Encode(file.getBlob().getBytes()) +
'\r\n--##########\r\n';
var draftBody = Utilities.base64EncodeWebSafe(raw, Utilities.Charset.UTF_8);
var params = {
method:"post",
contentType: "application/json",
headers: {"Authorization": "Bearer " + service.getAccessToken()},
muteHttpExceptions: true,
payload: JSON.stringify({"message": {"raw": draftBody}})
};
var resp = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/me/drafts", params);
Logger.log(resp)
}