Gmail snooze script

Jun 13, 2016 JavaScript Networking

You can easily create a Google Apps Script that will allow you to move messages into a set series of labels which will then automatically move them back into your INBOX on that particular day. This script is based initially on this post, however I preferred to use days of the week rather than “1[7] days” labels. I also added an “Next Month” option as I find that I often want to “snooze” an email until the following month.

What this does is create a label structure in your Gmail (Google Apps works too) like:

Snoozed/
  1 - Monday
  2 - Tuesday
  3 - Wednesday
  4 - Thursday
  5 - Friday
  6 - Saturday
  7 - Sunday
  Next Month

Then simply move any message you want snoozed until a particular day (in the next 7 days), or next month into one of those underlying labels, and somewhere between midnight and 1am on that day the messages will be moved back into your INBOX.

The reason the days are numbered is simply to keep the days of the week in order (Gmail lists labels alphabetically). Please note that this script is tested in English only in both the regular Gmail as well as Google Apps. If you require a different language then you will have to rename sections of the script. You can also delete unused labels (for instance if you never use Saturday or Sunday), however if you wish to recreate them then the naming is important, including the leading number (so Sunday should always be 7 - Sunday).

Script setup

In your Google Drive, first ensure that you can add a “Google Apps Script” type document. If you don’t have that option, right click in your files list, More => Connect more apps and then search for “Google App Scripts”.

Create the Code.gs

Then right click and add a new Google Apps Script.

Replace the contents of Code.gs with the following:

/**
 * Gmail Snooze Script
 * Handy info on:
 * - https://gmail.googleblog.com/2011/07/gmail-snooze-with-apps-script.html
 * - https://developers.google.com/apps-script/reference/gmail/gmail-app#methods
 * - https://developers.google.com/apps-script/guides/triggers/installable#managing_triggers_programmatically
 */

var MARK_UNREAD = false;
var ADD_STAR = false;
var SNOOZED_FOLDER = "Snoozed";
var DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var NEXT_MONTH = "Next Month";

/**
 * Create the labels we’ll need for snoozing and attach Gmail account if needed
 */
function GmailLabelSetup() {
  GmailApp.createLabel(SNOOZED_FOLDER);
  var sort_order = 0;
  for (var i in DAYS) {
    sort_order = sort_order + 1;
    GmailApp.createLabel(SNOOZED_FOLDER + "/" + sort_order + " - " + DAYS[i]);
  }
  GmailApp.createLabel(SNOOZED_FOLDER + "/" + NEXT_MONTH);
  // Set up triggers
  createTriggers();
}

/**
 * Run the Unsnoozed script to automatically move emails in matching folder to your INBOX 
 */
function DailyUnsnooze() {
  // Used to get the current day of the week
  var weekdays = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
  var d = new Date();
  
  // Calculate today's day of the week
  var current_weekday = weekdays[d.getDay()];
  
  // Snoozed label name
  var snoozed_label_name = SNOOZED_FOLDER + "/" + (DAYS.indexOf(current_weekday) + 1) + " - " + current_weekday;
  
  snoozed_label = GmailApp.getUserLabelByName(snoozed_label_name);
  
  if (!snoozed_label) {
    return;
  }
  
  email_threads = null;
  
  while(!email_threads || email_threads.length == 100) {
    
    email_threads = snoozed_label.getThreads(0, 100);
    
    if (email_threads.length > 0) {
      GmailApp.moveThreadsToInbox(email_threads);
      
      // Add star if required
      if (ADD_STAR) {
        for (var i in email_threads) {
          var thread = email_threads[i];
          if (!thread.hasStarredMessages()) {
            var message = thread.getMessages()[0]; // get first message
            message.star(); // star the message
          }
        }
      }
      
      // Mark unread
      if (MARK_UNREAD) {
        GmailApp.markThreadsUnread(email_threads);
      }
      
      // Remove from Snoozed/[day] label
      snoozed_label.removeFromThreads(email_threads); // remove snoozed label
    }
  }
}

/**
 * Run the Unsnoozed script to automatically move emails in matching folder to your INBOX 
 */
function MonthlyUnsnooze() {
  // Snoozed label name
  var snoozed_label_name = SNOOZED_FOLDER + "/" + NEXT_MONTH;
  
  snoozed_label = GmailApp.getUserLabelByName(snoozed_label_name);
  
  if (!snoozed_label) {
    return;
  }
  
  email_threads = null;
  
  while(!email_threads || email_threads.length == 100) {
    
    email_threads = snoozed_label.getThreads(0, 100);
    
    if (email_threads.length > 0) {
      GmailApp.moveThreadsToInbox(email_threads);
      
      // Add star if required
      if (ADD_STAR) {
        for (var i in email_threads) {
          var thread = email_threads[i];
          if (!thread.hasStarredMessages()) {
            var message = thread.getMessages()[0]; // get first message
            message.star(); // star the message
          }
        }
      }
      
      // Mark unread
      if (MARK_UNREAD) {
        GmailApp.markThreadsUnread(email_threads);
      }
      
      // Remove from Snoozed/[day] label
      snoozed_label.removeFromThreads(email_threads); // remove snoozed label
    }
  }
}

/**
 * Set up schedulars
 */
function createTriggers() {
  // Delete old triggers
  var allTriggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < allTriggers.length; i++) {
    ScriptApp.deleteTrigger(allTriggers[i]);
  }

  // Trigger every day
  ScriptApp.newTrigger('DailyUnsnooze')
    .timeBased()
    .everyDays(1)
    .atHour(0)
    .create();

  // Trigger every month
  ScriptApp.newTrigger('MonthlyUnsnooze')
    .timeBased()
    .onMonthDay(1)
    .atHour(0)
    .create();
}

/**
 * Called via published URL
 */
function doGet() {
  GmailLabelSetup();
  return HtmlService
      .createTemplateFromFile('Setup')
      .evaluate()
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

Create the Setup.html

Then go to File => New => Html file and give it the name Setup.html. This will be the “landing page” if the script is published. Paste the following into that new Setup.html file:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
   <h1>Gmail Snooze Script Installed</h1>
   <p>The script has been installed and the necessary labels have been created in your Gmail.</p>
   <p>To snooze any email, simply move it to one of the subdirectories.</p>
  </body>
</html>

Install the script

Select the Code.gs file, and then from the main menus select GmailLabelSetup. This will first ask you for access to your Gmail, after which it will create those labels automatically and install the triggers (scheduler) to run daily & monthly.

Additional notes

There are some options at the beginning of the Code.gs script which you can modify. Two worth mentioning are

var MARK_UNREAD = false;
var ADD_STAR = false;

This allows the script to either star messages when they get moved back to your INBOX, or mark them as unread.

Feel free to modify the code as you see fit, but this will get you started!

Comments