Schedule Gmail Emails with Google Script

Published in: Gmail - Google Apps Script

You can compose an email message in Gmail now, let it stay there in the Drafts folder and have it sent automatically at a specified data and time with the help of Google Scripts.

The Schedule GMail script imports the draft message in a spreadsheet, you specify the send data and time and it creates corresponding time-driven triggers to automate delivery.

/* Written by Amit Agarwal of */

function initialize() {

    /* Clear the current sheet */
    var sheet = SpreadsheetApp.getActiveSheet();
    sheet.getRange(2, 1, sheet.getLastRow() + 1, 5).clearContent();

    /* Delete all existing triggers */
    var triggers = ScriptApp.getProjectTriggers();
    for (var i = 0; i < triggers.length; i++) {
        if (triggers[i].getHandlerFunction() === "sendMails") {

    /* Import Gmail Draft Messages into the Spreadsheet */
    var drafts = GmailApp.getDraftMessages();
    if (drafts.length > 0) {
        var rows = [];
        for (var i = 0; i < drafts.length; i++) {
            if (drafts[i].getTo() !== "") {
                rows.push([drafts[i].getId(), drafts[i].getTo(), drafts[i].getSubject(), "", ""]);
        sheet.getRange(2, 1, rows.length, 5).setValues(rows);

/* Create time-driven triggers based on Gmail send schedule */
function setSchedule() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var data = sheet.getDataRange().getValues();
    var time = new Date().getTime();
    var code = [];
    for (var row in data) {
        if (row != 0) {
            var schedule = data[row][3];
            if (schedule !== "") {
                if (schedule.getTime() > time) {
                } else {
                    code.push("Date is in the past");
            } else {
                code.push("Not Scheduled");
    for (var i = 0; i < code.length; i++) {
        sheet.getRange("E" + (i + 2)).setValue(code[i]);

function sendMails() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var data = sheet.getDataRange().getValues();
    var time = new Date().getTime();
    for (var row = 1; row < data.length; row++) {
        if (data[row][4] == "Scheduled") {
            var schedule = data[row][3];
            if ((schedule != "") && (schedule.getTime() <= time)) {
                var message = GmailApp.getMessageById(data[row][0]);
                var body = message.getBody();
                var options = {
                    cc: message.getCc(),
                    bcc: message.getBcc(),
                    htmlBody: body,
                    replyTo: message.getReplyTo(),
                    attachments: message.getAttachments()

                /* Send a copy of the draft message and move it to Gmail trash */
                GmailApp.sendEmail(message.getTo(), message.getSubject(), body, options);
                sheet.getRange("E" + (row + 1)).setValue("Delivered");
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Gmail - Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube