Blog

It's minimal, but I'm posting things.

Automatically snoozing all incoming emails until the next day in Gmail

Often enough, you get emails asking you for things, and because many of those demands get solved by the sender within a few hours, it makes sense to read them only the next day.

And so, automatically snoozing all emails until the next day has been a good practice for me, and has prevented on many occasions wasted work.

So here is a script for Google Gmail to do exactly that.

/**
 * Gmail Snooze Script - Improved Version
 * 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 = true;
var ADD_STAR = false;
var SNOOZED_FOLDER = "SnoozedUntilTomorrow";

// EXCLUSIONS: For instance, list your own email addresses here
var EXCLUDED_SENDERS = [];

function GmailLabelSetup() {
  if (!GmailApp.getUserLabelByName(SNOOZED_FOLDER)) {
    GmailApp.createLabel(SNOOZED_FOLDER);
  }
  createTriggers();
}

/**
 * Daily Unsnooze with safety check for sent/scheduled messages
 */
function DailyUnsnooze() {
  var snoozed_label = GmailApp.getUserLabelByName(SNOOZED_FOLDER);
  if (!snoozed_label) return;
  var threads = snoozed_label.getThreads(0, 100);
  
  while (threads.length > 0) {
    var validThreads = [];
    
    for (var i = 0; i < threads.length; i++) {
      var thread = threads[i];
      var lastMessage = thread.getMessages().pop();
      var sender = lastMessage.getFrom().toLowerCase();
      
      // SAFETY CHECK: Skip if it's from an excluded sender
      var isMe = EXCLUDED_SENDERS.some(function(email) {
        return sender.indexOf(email) !== -1;
      });
      if (isMe) {
        // Just remove the label so it stops being "snoozed"
        thread.removeLabel(snoozed_label);
        continue;
      }
      
      validThreads.push(thread);
    }
    if (validThreads.length > 0) {
      GmailApp.moveThreadsToInbox(validThreads);
      if (ADD_STAR) {
        validThreads.forEach(function(t) {
          t.getMessages()[0].star();
        });
      }
      if (MARK_UNREAD) {
        GmailApp.markThreadsUnread(validThreads);
      }
      snoozed_label.removeFromThreads(validThreads);
    }
    
    // Get next batch
    threads = snoozed_label.getThreads(0, 100);
  }
}

function createTriggers() {
  var allTriggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < allTriggers.length; i++) {
    ScriptApp.deleteTrigger(allTriggers[i]);
  }
  // Primary unsnooze trigger
  ScriptApp.newTrigger('DailyUnsnooze')
    .timeBased()
    .everyDays(1)
    .atHour(7)
    .nearMinute(45)
    .inTimezone('Europe/Paris')
    .create();
}

function doGet() {
  GmailLabelSetup();
  return HtmlService.createTemplateFromFile('Setup')
      .evaluate()
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

Published on 2026-01-31T18:30:02.571939Z
Last updated on 2026-01-31T18:31:56.524371Z