Send Draft Emails with the Gmail API

The Gmail API, like the GmailApp service, can help you interact with your Gmail mailbox through Google Apps Script.

The first sample snippets fetches a list of draft emails residing in your Gmail mailbox while the other one will send one of these draft messages via the Gmail API itself. All you have to specify is the Gmail Message ID.

// Credit: https://gist.github.com/mogsdad/6515581

function getGmailDrafts() {
  var params = {
    method: 'get',
    muteHttpExceptions: true,
    headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },
  };

  var resp = UrlFetchApp.fetch('https://www.googleapis.com/gmail/v1/users/me/drafts', params);

  var drafts = JSON.parse(resp.getContentText()).drafts;

  for (var i = 0; i < drafts.length; i++) {
    Logger.log(drafts[i].message);
  }
}

function sendGmailDraft() {
  var draftID = '12345';

  var params = {
    method: 'post',
    contentType: 'application/json',
    headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },
    muteHttpExceptions: true,
    payload: JSON.stringify({ id: draftID }),
  };

  var resp = UrlFetchApp.fetch('https://www.googleapis.com/gmail/v1/users/me/drafts/send', params);

  Logger.log(resp.getResponseCode());

  Logger.log(resp.getContentText());
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻