1

I try to automatically attach a file from my Google Drive (so ideally with the file id) in my Gmail draft created with Google Apps Script and GMail API. I use the syntax below. Can I do that easily? Creating the draft works great by the way.

Thanks! Chris

  function createDraft() {

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

  var htmlBody = 'Howzit';

  var raw = 
      'Subject: Howzit\n' + 
      'To: [email protected]\n' +
      'Content-Type: text/html; charset=UTF-8\r\n' +
      '\r\n' + htmlBody;

  var draftBody = Utilities.base64Encode(raw, Utilities.Charset.UTF_8).replace(/\//g,'_').replace(/\+/g,'-');

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

}

1 Answer 1

3

How about following sample script? This is a very simple script for attaching a file to a draft. So please modify this to your environment.

In order to use this script, please enable Gmail API at API console. And please import file ID to fileId in the script.

Sample script :

function createDraft() {
  var fileId = "### file id ###";
  var file = DriveApp.getFileById(fileId);
  var forScope = GmailApp.getInboxUnreadCount();
  var htmlBody = 'Howzit';
  var raw = 
      'Subject: Howzit\r\n' + 
      'To: [email protected]\r\n' +
      'Content-Type: multipart/mixed; boundary=##########\r\n\r\n' +
      '--##########\r\n' +
      'Content-Type: text/html; charset=UTF-8\r\n\r\n' + htmlBody + '\r\n' +
      '--##########\r\n' +
      'Content-Type: ' + file.getMimeType() + '; charset=UTF-8; name="' + file.getName() + '"\r\n' +
      'Content-Disposition: attachment; filename="' + file.getName() + '"\r\n' +
      'Content-Transfer-Encoding: base64\r\n\r\n' + Utilities.base64Encode(file.getBlob().getBytes()) +
      '\r\n--##########\r\n';
  var draftBody = Utilities.base64EncodeWebSafe(raw, Utilities.Charset.UTF_8);
  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)
}

Result :

{
  "id": "#####",
  "message": {
    "id": "#####",
    "threadId": "#####",
    "labelIds": [
      "DRAFT"
    ]
  }
}

Image :

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks a lot, works as a beast, wasn't able to find it anywhere. Thanks!
@Chris Rutte Welcome. Thank you, too.
Quick extra question, what if I want to add two files to the e-mail? Thanks in advance!
@Chris Rutte Please loop the part which attaches a file by separating using '--##########\r\n'.
Thanks, will try it this afternoon or tonight!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.