Creating "Gmail Snooze" Extension With Google Apps Script Code

If you're using Gmail and deals with a lot of email, you can either 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." Or, better go for "snooze an email". "To snooze an email means to archive […]

If you're using Gmail and deals with a lot of email, you can either 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." Or, better go for "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," wrote Corey Goldfeder Google software engineer on the Apps Script.

With Apps Script you can extend Gmail yourself to add this functionality and a lot more.

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'll reappear in your inbox, unarchived. If you want the messages to reappear as unread, just change "MARK_UNREAD" at the top to be "true"," explains Goldfeder.

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.

Apps Script code for a "Gmail Snooze" extension:

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);
      }  
    }
  }
}

"To setup a Apps Script, just 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'll 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," added Goldfeder.

Watch the video demonstration of "Gmail Soonze":

[Source: Google Apps Developer Blog]