Day 5: Made a script to simplify my email

I often accumulate several hundred emails in my inbox at one time, most of them unread. The last time this happened, I had to go through over 400 emails to bring my inbox to 12 (not quite inbox zero, but in that moment at 2AM on a Friday morning, I felt it was close enough).

The largest chunk of those emails are promotions that are useful to me for a week or less, but less than worthless afterward. For example, Harris Teeter weekly deals or Express sales. I just spent about twenty minutes selecting all the old promotional stuff and deleting. The first batch of deletions was almost 150. Kinda boring and repetitive. Perfect for machines! So I looked up a way to automate it, and found this lifehacker article. I based my script on code linked in the article, but mine is better due to some simple optimization (looping through emails one-by-one? What is this, COMP101?).

The first part of the system is to categorize incoming mail with a label. Identify all the emails that you can always safely delete without reading if they’re too old (I keep finding new ones; it’s an ongoing process). Here’s one of my shorter filter definitions:

Matches: ((from: update@facebookmail.com 
    OR notification@facebookmail.com OR noreply@youtube.com
    OR noreply-d6bb4190@plus.google.com 
    OR info@twitter.com) AND category:social)
Do this: Apply label "expires"

Then run the following script every night. Google Apps Scripts does all the hard work for you–you just need to know JavaScript and be able to read a bit of the API. This will delete all the “expires” emails that are in my inbox and older than 10 days.

function cleanUp() {
  var delayDays = 10,
    date = Utilities.formatDate(new Date(new Date().setDate(
        new Date().getDate() - delayDays)), 
        "GMT", "yyyy/MM/dd"),
    query = "in:inbox and label:expires and before:" + date,
    max = 5,
    start = 0;
  
  do {
    var threadsSubset = GmailApp.search(query, start, max);
    GmailApp.moveThreadsToTrash(threadsSubset);
  } while (threadsSubset.length == max);
}

Yes, setting the date was a bit inefficient (three Date objects), but I actually saw that as an example in Google’s documentation. I chose to use it, mostly for giggles.

The Do..While loop nesting the search method was necessary to prevent issues with a too-large search result. Since this script runs every night, it’s mostly just there for good form. I can’t see myself receiving more than 10 promotional emails per day, and I’m sure the threshold where problems occur is much higher than 5.

So there you have it. A technical post by yours truly. And now I’m only one day behind in NaBloWriMo.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s