Seb P
Seb P

Reputation: 687

Create draft mail using Google apps script

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

Upvotes: 11

Views: 13458

Answers (4)

 GhostKU
GhostKU

Reputation: 2108

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("[email protected]", "current time", "The time is: " + now.toString());

Upvotes: 11

Mogsdad
Mogsdad

Reputation: 45710

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: [email protected]\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" ]
   *   }
   * }
   */
}

Upvotes: 17

Allen Hancock
Allen Hancock

Reputation: 139

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

Upvotes: 1

Srik
Srik

Reputation: 7957

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

Upvotes: 1

Related Questions