使用Google应用脚本创建草稿邮件

Seb*_*b P 11 gmail google-apps-script

我想知道是否可以使用Google Apps脚本创建草稿邮件.如果是的话,怎么可能呢?

此致,塞巴斯蒂安

Mog*_*dad 15

此时,无法创建出现在Drafts文件夹中的新邮件.之前已请求此功能 - 请参阅问题985.如果您有兴趣接收任何更新,请访问并加注问题.

编辑:虽然Google Apps脚本仍然不支持,但您可以使用GMail API创建草稿,用于getOAuthToken()进行身份验证(2014年2月推出).2014年6月在API中添加了草稿支持,并在上述问题的评论29中显示了其在GAS中使用的示例.为方便起见,此处转载的代

function createDraft() {

  var forScope = GmailApp.getInboxUnreadCount(); // needed for auth scope

  var raw = 
      'Subject: testing Draft\n' + 
      //'To: test@test.com\n' +
      'Content-Type: multipart/alternative; boundary=1234567890123456789012345678\n' +
      'testing Draft msg\n' + 
      '--1234567890123456789012345678--\n';

  var draftBody = Utilities.base64Encode(raw);

  var params = {method:"post",
                contentType: "application/json",
                headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
                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.getContentText());
  /*
   * sample resp: {
   *   "id": "r3322255254535847929",
   *   "message": {
   *     "id": "146d6ec68eb36de8",
   *     "threadId": "146d6ec68eb36de8",
   *     "labelIds": [ "DRAFT" ]
   *   }
   * }
   */
}
Run Code Online (Sandbox Code Playgroud)


Gh*_*KU 7

谷歌在2017年9月增加了对草稿的支持.来自文档:

// The code below creates a draft email with the current date and time.
var now = new Date();
GmailApp.createDraft("mike@example.com", "current time", "The time is: " + now.toString());
Run Code Online (Sandbox Code Playgroud)