Auto-filter your sent Gmail emails

I’m a bit of an email nerd.

Let me clarify. I spend a lot of time in my email client (Gmail browser), send and receive a lot of emails to a lot of different people about a lot of different things. I’ve also learned that unless I implement processes, I forget things (I always keep my phone in my left pocket and my wallet and keys in my right). I also love to automate processes whenever possible (that’s part of what I love about coding!).

I recently realised that the obvious twist to put on my Gmail labels/filters setup was a ‘Follow Up’ label that I’d apply to mails that would need my attention if they didn’t get dealt with (you sometimes want to wait a while before you nag someone for a reply, especially if you don’t know them…).

After choosing a nice bright colour and pulling the labelled threads into a new section on my Multiple Inbox I went ahead and labelled the messages that had made me think about this in the first place, while thinking about rules that would accurately capture all the emails I’d forget to chase down.

It’s relatively easy to remember to label an ongoing discussion, and it’s pretty easy to unlabel something you don’t need a label on, but labelling a message as you send it is hard (or impossible) to do, and it’s also the one you’re least likely to remember later.

I realised I’d need a filter to auto-label these messages, and I came up with the following matching criteria:

  • A message I’d sent…
  • that hadn’t been replied to…
  • that was older than a couple of days, but newer than last week

Imagine my frustration as I realised that I wouldn’t be able to automate my process because — of course, dammit! — you can’t apply a filter to messages as they’re sent. I’ve tried using Boomerang, but it didn’t stick, mainly because there’s no automatic part to it. I had to make this work.

Well of course there’s a way. As I’ve been realising increasingly of late, it’s using Google Apps Scripts. In a nutshell, you write a script to search for certain messages in your inbox, iterate over them checking anything you couldn’t search for and applying your label – and you then trigger the search on a timer. Amazing.

Here’s the how-to:

1. Specify your initial search. I couldn’t figure out a way to get messages without replies, so I left that out for now. You’ll also need to search only for messages on a single day (you’ll see why in a moment), and make sure to exclude chat messages – I ended up with:

in:sent older_than:2d newer_than:3d -in:chats

2. Open up a new script. If you’ve never done this before, follow the beginning of my previous post, or just google it (it’s easy).

3. Copy and paste the following code into your script:

function followUpUnreplied() {
  // Every thread in your Inbox that is read, older than two days, and not labeled "delete me".
  var threads = GmailApp.search('in:sent older_than:2d newer_than:3d -in:chats');
  for (var i = 0; i < threads.length; i++) {
    if (threads[i].getMessageCount() < 2) {
    // looks like an unreplied-to message
      threads[i].addLabel(GmailApp.getUserLabelByName("Follow Up"))
    }
  }
}

What you’re doing here is running your search, then looping through the results (each of which is a “thread” – a conversation). You check each thread for the number of messages and if there’s only 1 you apply the label to it – simples!

4. Next, you need to set your script to run automatically every day (which is why we only searched for messages from a single day in the past). In the script editor window, go to Resources > Current project’s triggers, which will open an overlay with an empty list on the editor page.

Select ‘Add a new trigger’ choose your function name ‘followUpUnreplied’ in this case, choose ‘Time-driven’, ‘day timer’, and select a time that suits you.

5. Lastly, run the script manually from Run menu item; the first time you run it it’ll need authorisation to access your account and you’ll need to do this manually. After that, you should be good to go!

Now you just have to remember to act on all those labels :)

6 thoughts on “Auto-filter your sent Gmail emails

  1. I have a less geeky, but none the less brilliant, solution – GTD Inbox. Uses the GTD methodology together with a bit of Inbox Zero, and includes adding labels to outgoing email. And it’s free. And amazing. And free.

    • I think I suffer from early adopter syndrome. I tried that years ago and it wasn’t that great, but GTD was the main impetus for the labeling in the first place. I’ll give it a go again: thanks for the tip :)

  2. Hey – nice work Marcel. I’m getting an error on the script tho…

    Missing ) after for-loop control. (line 4, file “Code”)

    Any ideas? Thanks for posting this! :)

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