Friday, July 29, 2011

Gmail Snooze with Apps Script

What can you do with a little bit of 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 (the internet is full of kittens), a lot of Googlers subscribe to the “inbox zero” philosophy. In inbox zero you try to keep your inbox empty of all but the emails you currently need to deal with.

What is Gmail Snooze?

In managing our inboxes, one feature that we really wanted was for Gmail to let you “snooze” an email. To snooze an email means to archive it for now, but to have it automatically reappear in the inbox at some specified time in the future. With Apps Script you can extend Gmail yourself to add this functionality and a lot more.

The Solution

Here is the Apps Script code for a “Gmail Snooze” extension. First some configuration details. Setting these variables determines whether “unsnoozed” mail gets marked as unread, and whether it gets its own special label.
var MARK_UNREAD = false;


The “setup” function creates a new “Snooze” label in your Gmail, along with 7 sublabels for snoozing for different lengths of time, and potentially an “Unsnoozed” label. Running this function will also prompt you to authorize the script to use Gmail. This function makes use of the “getLabelName” helper function, which will be used by the code below.
function getLabelName(i) {
  return "Snooze/Snooze " + i + " days";

function setup() {
  // Create the labels we’ll need for snoozing
  for (var i = 1; i <= 7; ++i) {

Moving the Snooze Queue

The “moveSnoozes” function moves messages one day forward in the queue, so that messages snoozed for 6 days are now snoozed for 5 days, etc. Messages in the 1-day label are moved back into the inbox, and potentially marked as unread. To make this work automatically, you’ll need to create a nightly event trigger to run “moveSnoozes”. See the more detailed instructions at the bottom of the post.
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
        } else {
          // Unless it’s time to unsnooze it
          if (MARK_UNREAD) {
          if (ADD_UNSNOOZED_LABEL) {
        // Move the threads out of "yesterday’s" label

Using 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. If you want the messages to reappear as unread, just change “MARK_UNREAD” at the top to be “true”.

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.

If you don't know how to setup a script, it's pretty simple. Create a new Google Spreadsheet, and choose "Script Editor" from the "Tools" menu. Paste in all of the code from above and 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 are done.

No comments:

Post a Comment

Share This: