Create draft mail using Google apps script

2019-03-15 11:00发布

I want to know if it's possible to create draft mail using Google Apps script. And if yes, how is it possible ?

Regards, Sebastien

4条回答
相关推荐>>
2楼-- · 2019-03-15 11:36

Google added support for generating drafts in September 2017. From the documentation:

// 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());
查看更多
Ridiculous、
3楼-- · 2019-03-15 11:37

I do this via Zapier now - it's fantastic.

See this zap http://zpr.io/fhmT

Google's documentation on the subject is here:

https://developers.google.com/gmail/api/guides/drafts

查看更多
再贱就再见
4楼-- · 2019-03-15 11:45

No. It is not possible to do so. See the documentation.

查看更多
趁早两清
5楼-- · 2019-03-15 11:54

At this point in time, there is no way to create a new message that appears in your Drafts folder. This functionality has been requested previously - see Issue 985. If you are interested in receiving any updates, visit and star the issue.

EDIT: While still not natively supported in Google Apps Script, you can create drafts by using the GMail API, using getOAuthToken() to authenticate (introduced Feb 2014). Drafts support was added to the API in June 2014, and an example of its use from GAS is shown in comment 29 of the above issue. Code reproduced here for convenience:

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" ]
   *   }
   * }
   */
}
查看更多
登录 后发表回答