Auto-filter your sent Gmail emails

Marcel Kornblum
Marcel Kornblum’s Posts
3 min readJun 24, 2013

--

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 :)

--

--

I like to make things I can eat or play with, mainly using meat and tech. Head of Creative Technology @bbhlondon.