Gmail Snooze with Apps Script

At Google, we all use email very heavily -- for communicating with other Googlers, for task management, and to mail around funny pictures of kittens. Because of the volume of email we all deal with, a lot of Googlers subscribe to the “inbox zero” philosophy where we try to keep our inboxes empty except for the messages we currently need to deal with.

What is Gmail Snooze?
One feature that some of us really wanted was for Gmail to let you “snooze” an email. Snoozing means archiving an email for now, but having it automatically reappear in the inbox at some specified time in the future. With Apps Script you can extend Gmail to add this functionality and a lot more yourself.



How to set it up
Even if you don't know how to write a script, it's pretty simple. Go to Google Docs and create a new spreadsheet, then choose "Script Editor" from the "Tools" menu. Paste in the following code:


var MARK_UNREAD = false;
var ADD_UNSNOOZED_LABEL = false;

function getLabelName(i) {
  return "Snooze/Snooze " + i + " days";
}

function setup() {
  // Create the labels we’ll need for snoozing
  GmailApp.createLabel("Snooze");
  for (var i = 1; i <= 7; ++i) {
    GmailApp.createLabel(getLabelName(i));
  }
  if (ADD_UNSNOOZED_LABEL) {
    GmailApp.createLabel("Unsnoozed");
  }
}

function moveSnoozes() {
  var oldLabel, newLabel, page;
  for (var i = 1; i <= 7; ++i) {
    newLabel = oldLabel;
    oldLabel = GmailApp.getUserLabelByName(getLabelName(i));
    page = null;
    // Get threads in "pages" of 100 at a time
    while(!page || page.length == 100) {
      page = oldLabel.getThreads(0, 100);
      if (page.length > 0) {
        if (newLabel) {
          // Move the threads into "today’s" label
          newLabel.addToThreads(page);
        } else {
          // Unless it’s time to unsnooze it
          GmailApp.moveThreadsToInbox(page);
          if (MARK_UNREAD) {
            GmailApp.markThreadsUnread(page);
          }
          if (ADD_UNSNOOZED_LABEL) {
            GmailApp.getUserLabelByName("Unsnoozed")
              .addToThreads(page);
          }          
        }     
        // Move the threads out of "yesterday’s" label
        oldLabel.removeFromThreads(page);
      }  
    }
  }
}
Then click the “Save” button and give it a name. In the dropdown labeled "Select a function to run," choose "setup" and click the blue run arrow to the left of it. This will ask you to authorize the script, and will create the necessary labels in your Gmail. Then go to the "Triggers" menu and choose "current script's triggers." Click the link to set up a new trigger, choosing the "moveSnoozes" function, a "time-driven" event, "day timer," and then "midnight to 1am." Click save and you’re done.

Using the Snooze Label in Gmail
To "snooze" a thread, use Gmail’s “Move To” button to move the thread into the "Snooze for X days" label and archive it. Every night, threads will move up through one day of the queue, and at the appointed number of days they will reappear in your inbox, unarchived.

Because this is an Apps Script, you can edit the code any way you like. If you’d like different snooze times or for unsnoozed messages to get starred, you can easily change the code. And if you have an even better idea for how to use Apps Script to improve Gmail, you can post it to our Gallery (Script Editor > Share > Publish Project) to share with the world.