MrGreggles
MrGreggles

Reputation: 6175

How to use the Google Apps Script code for creating a Draft email (from 985)?

In Issue 985, a developer named "yurec" (perhaps) in comment #29 provides some code to create a draft (text) email in Google Apps Script. Some others thank him for the good work and say that it's tested and working.

I'm a bit stumped as to how to use it and which parts of the code need changing (like "id" and "threadId"?).

Anyway, ideally I guess the usage would be like:

MailApp.createDraft(emailTo, subject, body, {'name':'Bob from Example Ltd'});

But whatever works is good with me. Thanks for any help here. Getting this to work will be fantastico.

Working Code - You just need to set up the Gmail API as per the selected answer

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
              }
            })
           };

Upvotes: 1

Views: 3934

Answers (2)

Piotr Magdziak
Piotr Magdziak

Reputation: 21

is it any solutions, to paste on this e-mail part of sheet? f.e.:

spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Podsumowanie - raport'), true); var range = sheet.getRange('A1:N46');

Upvotes: 0

Cyrus Loree
Cyrus Loree

Reputation: 847

You only need to setup the Gmail API in the console

In Script Editor:

  • Resources > Advanced Script Services (screenshot 1)
  • click on Google Developers Console link at the bottom of the window (screenshot 2)

In Developers Console:

  • Scroll down and find Gmail API and turn it on. (screenshot 3)

Close the Developers Console tab

Click Ok in the Advanced Google Services modal

Authorize the app, and you're all set.


Here is the code I tested with

function createDraft() {
  try{
  var forScope = GmailApp.getInboxUnreadCount(); // needed for auth scope

  var raw = 
      'Subject: testing Draft from Apps Script\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());
  }catch(err){
    Logger.log(err.lineNumber + ' - ' + err);
  }
}

Screenshot 1 Advanced Services Menu

Screenshot 2 Advanced Services Modal

Screenshot 3 Developers Console

Upvotes: 3

Related Questions