CrazyEngineers
  • Many times we want to check an email but we don't have the time right now. In this case, we can “snooze” that 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 we can extend Gmail to add this functionality and a lot more.

    [video=youtube;p2MU5IoaveA]https://www.youtube.com/watch?v=p2MU5IoaveA&feature=player_embedded[/video]

    Here's how to do this:

    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.

    Note: After you run the setup() function, the labels will be created in Gmail. However, you may have to refresh your Gmail window to see the labels.

    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");
      }
    }
    To move 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 #-Link-Snipped-# to run “moveSnoozes”.

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

    Try it out & post here about how it works.
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • ISHAN TOPRE

    MemberJul 30, 2011

    Just thinking if we can schedule our outgoing emails too. Suppose I write a mail and I want to send it at 12 pm. Can I do that? Is there something like a snooze there too?
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register