1

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: my.test.account@gmail.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
              }
            })
           };
MrGreggles
  • 6,113
  • 9
  • 42
  • 48
  • I copied the code from that post into an Apps Script script file, and the "id" and "threadId" text is stuff that is commented out of the code. It's not code to be used, it's an example of what the return value of the `UrlFetchApp.fetch("");` HTTP Request statement is. – Alan Wells Aug 19 '14 at 20:03
  • There is no `createDraft` method of the `MailApp` Class. [Documentation MailApp](https://developers.google.com/apps-script/reference/mail/mail-app) If you want to call the createDraft function that is shown in the post you are referencing, you would simply do that as you would call any JavaScript function. And there are no arguments in that createDraft function to pass anything to. You could add arguments, but that's just basic programming knowledge. – Alan Wells Aug 19 '14 at 20:07
  • lol, yeah I get it now. But bummer, it didn't work. The Unread email count is correct and each value seems to be read correctly (via the debug window) but unfortunately no draft. – MrGreggles Aug 20 '14 at 07:11
  • Agh, checked the logs which reminded me that the Gmail API needs activating. :-) – MrGreggles Aug 22 '14 at 07:23
  • Now I see that there is Users.drafts class in the Gmail API (https://developers.google.com/gmail/api/v1/reference/users/drafts). What steps would I need to take to get this up and running? Bounty awaiting ~ – MrGreggles Aug 22 '14 at 07:39
  • Oh, sick - it's working. So happy. This opens up all sorts of possibilities for our business automation. No bounty required, never mind... – MrGreggles Aug 22 '14 at 09:32
  • @GreggCleland Then please delete your question. I lost a lot of time reading this until I saw this hidden comment! – Henrique G. Abreu Aug 22 '14 at 13:58
  • @Henrique: Sry for that; change made. – MrGreggles Aug 23 '14 at 06:24

2 Answers2

3

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: cyrus@mydomain.net\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

Cyrus Loree
  • 837
  • 6
  • 7
  • Cheers Cyrus. I managed to stumble my way thru it but hope it'll be useful for someone else. And the bounty is yours! – MrGreggles Aug 23 '14 at 06:26
0

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