使用媒体上传方法在Appscript中发送带有附件的电子邮件

时间:2019-01-10 07:42:49

标签: google-apps-script gmail-api google-app-maker google-apis-explorer

我已经使用“媒体上传”方法创建了消息草稿 使用下面的代码

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参数,或者上述代码出了什么问题?

谢谢。

1 个答案:

答案 0 :(得分:0)

  • 您已经能够正确创建电子邮件草稿。
  • 您要通过使用Google Apps脚本直接请求到Gmail API的端点来发送创建的邮件草稿。

如果我的理解是正确的,那么该修改如何?

修改点:

  • 使用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
  })
});

注意:

  • 此修改假设您已经能够使用访问令牌来使用Gmail API。
  • 如果草稿邮件的格式不完整,则在发送草稿邮件时,可能会发生错误。

参考:

如果我误解了你的问题,我深表歉意。

编辑:

当您要创建包含附件文件的电子邮件草稿时,此示例脚本如何?来自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)
}