Automate parts of managing your Gmail

Did you know you can automate the heck out of Gmail?

Posted by Janne Cederberg on May. 16, 2020
Categories: Automation
Tags: email, youtube
Reading time: approx. 3 minute(s)

Email is a great tool. But it’s not such a great master. Managing one’s email inbox has become an art of its own with even a Google TechTalk on the topic:

In case you use a Gmail mailbox (either with a @gmail.com domain or some other Google-hosted domain), did you know you can use Google AppScripts to automate tons of things for example regarding your email inbox.

Goal

Let’s create an automation for our Google-hosted inbox that will archive all email threads that fulfill the following two criterion:

  1. The newest email in the email thread is older than X days old
  2. The status of the email thread is read

Instructions

  1. Using your browser log into https://script.google.com using your Google account.
  2. Create a new project
  3. Copy the AppScript code listed below into the code view and save
  4. Return to the browser tab that contains the listing of your projects at https://script.google.com
  5. On the right side of the desired project, click the three vertical dots
  6. Select Triggers (in Finnish: Käynnistimet)
  7. Click on Add trigger (in Finnish: Lisää triggeri)
  8. Configure the settings for how often/when you want to trigger the code you defined in step 3
  9. Save

AppScript code for above step 3

/**
 * Let's clean up the Gmail inbox so that all messages that are:
 *   1. older than X days and
 *   2. that have been read
 * get automatically and for example daily archived.
 */
function cleanInbox() {
  // Set threshold in days; read messages in inbox and older than X days will be archived
  var thresholdDays = 14;

  // GmailApp.search's max pagesize is 500, we'll stay well below that
  var pageSize = 100;

  var archiveOlderThanDate = new Date(new Date() - thresholdDays * 24 * 60 * 60 * 1000);
  var pageIndex = 0;

  do {
    /**
     * Get message threads that are in inbox, are read and where the *first* message of the
     * thread is older than thresholdDays old. Notice that even though the first message of
     * the thread would be older than thresholdDays, the newest message in the thread might
     * be much more recent, but on the flipside, if the first message of a thread is newer
     * than thresholdDays, then the latest msg of the thread also must be so and hence it
     * would be a waste of time looking at such threads.
     */
    var threads = GmailApp.search('in:inbox is:read older_than:' + thresholdDays + 'd', pageIndex * pageSize, pageSize);
    for (t in threads) {
      var latestDate = threads[t].getLastMessageDate();
      if (latestDate < archiveOlderThanDate) {
        threads[t].moveToArchive();
      }
    }
    pageIndex += 1;
  } while (threads.length == pageSize);
}

When is it worth the time to automate?

This is a great summary of when automating is worth it, considered across a 5 year time:

XKCD: Is it worth the time?

comments powered by Disqus