How I cleared my inbox of calendar updates

You just sent out a calendar invite to your team / department / office branch and now the accepted / rejected emails start pouring in.

We all tried it. You invited a semi-large group of people to a meeting, and now you are getting spammed with calendar responses.

I finally got enough and created a solution, which Google really should have just built into Gmail from the beginning.

We want those updates. We want our calendar to get updated with information on who is joining our meeting. What we don't want is an inbox full of information which is meant for a machine and not for a human. We just need to see how many people are attending in our calendar, and the calendar has a perfectly fine mechanism for that already:

Finding the calendar mail identifiers

So I started digging for some unique indicator that an email is a calendar update. The subject is relatively dynamic with only the "accepted" or "rejected" strings being constant but those seem too generic to filter on. Too many false positives.

So I had a look at the mail headers:

Delivered-To: anders.borch@example.com
Received: by 2002:a5d:4082:0:0:0:0:0 with SMTP id o2csp1438579wrp;
        Thu, 18 Jun 2020 03:46:50 -0700 (PDT)
X-Received: by 2002:a2e:9bd2:: with SMTP id w18mr1914344ljj.448.1592477210801;
        Thu, 18 Jun 2020 03:46:50 -0700 (PDT)

[many, many headers removed for brevity]

MIME-Version: 1.0
X-Received: by 2002:a2e:891a:: with SMTP id d26mr1988520lji.384.1592477210541;
 Thu, 18 Jun 2020 03:46:50 -0700 (PDT)
Reply-To: Alex <alex@example.com>
Sender: Google Calendar <calendar-notification@google.com>
Auto-Submitted: auto-generated
Message-ID: <00000000000072a2c505a8597e15@google.com>
Date: Thu, 18 Jun 2020 10:46:50 +0000
Subject: Accepted: All hands @ Fri Jun 26, 2020 16:30
 - 18:30 (CEST) (anders.borch@example.com)
From: Alex <alex@example.com>
To: anders.borch@example.com
Content-Type: multipart/mixed; boundary="00000000000072a2b505a8597e14

Neither From, To, or Subject is of much use.

After going through the headers, I noticed that Sender looks useful.

Creating a filter

Annoyingly, Sender is not one of the fields that can be filtered natively in gmail.

Luckily, Google Apps Script comes to the rescue.

I created a script which identifies calendar mails based on the Sender header and applies a Calendar label to those mails and removes them from the inbox.

Step one is to enumerate all recent messages. The granularity on "recent" by the Gmail API goes down to 1 hour, so we process those messages.

function processInbox() {
  // process all recent threads in the Inbox
  var threads = GmailApp.search("newer_than:1h");
  for (var i = 0; i < threads.length; i++) {
    // get all messages in a given thread
    var thread = threads[i];
    var messages = thread.getMessages();
    for (var j = 0; j < messages.length; j++) {
      var message = messages[j];
      if (isCalendarMessage(message)) {
        labelThread(thread);
        break;
      }
    }
  }
}

Next, add the message classification, which is done simply by looking for the existence of a Sender header where the content is Google Calendar:

function isCalendarMessage(message) {
  var body = message.getRawContent();
  if (body.indexOf("Sender: Google Calendar <calendar-notification@google.com>") > -1) {
    return true;
  } else {
    return false;
  }
}

Finally, apply the Calendar label and archive the thread:

function labelThread(thread) {
  var label = GmailApp.getUserLabelByName("Calendar");
  thread.addLabel(label);
  GmailApp.moveThreadToArchive(thread);
}

Run the script to ensure that everything works. It should ask for authorization to access your gmail account on the first run.

Once everything works, add a trigger to ensure that it gets run automatically when there is a calendar update:

Now, my inbox is finally free from calendar spam.