Extending Gmail with Regex

The Ask

  • Emergency Relief Fund needs to get bail money back from the court system
  • They sign up for eTrack notifications, but the majority of them are junk
  • Can we filter out emails that have a next appearance date scheduled so that a human doesn't have to open every single email and figure out if it's useful or not?

The problem

Gmail filters don't enable regex checking

The solution

Google Apps Script is surprisingly simple

const regex = new RegExp(/Next Appearance: \d{2}\/\d{2}\/\d{4}/)

const threads = GmailApp.getInboxThreads()

threads.forEach(thread => {
    thread.getMessages().forEach(email => {
    	const body = email.getBody()
    	if(!regex.test(body)) {
     		email.forward('forwarding-email@gmail.com')
    	}
    })
    thread.moveToArchive()
})

Minor caveat

To my knowledge, there is no "on email received" event you can tap into in Apps Script, so I:

  • Set up a trigger (you can specify a time interval aka run this every hour or day etc)
  • In my script I archive emails I've already filtered so that I'm not indefinitely refiltering the same emails over and over again
Made with Slides.com