How to use "not" in Gmail filters?

pterandon picture pterandon · Aug 25, 2015 · Viewed 65.9k times · Source

I need to create a Google App Script to respond to machine generated emails, except those also sent to one colleague or contain a particular topic outside my responsibility.

I've been trying to use the GmailApp.search API which includes all mail from user X but NOT those emails which are also addressed to user Y NOR those containing "junk_term". I want the emails archived and to skip the inbox.

I created a query like so:

 "from:([email protected]) -{[email protected] OR junk_term}"

But it appears not to return any results.

Answer

pterandon picture pterandon · Aug 26, 2015

Fixing this problem requires understanding two things:

1) If a label has been placed on an existing email by a filter, the label stays even if that filter were deleted. One must delete the label itself, and then re-create a new filter in order to get your edits to "take".

2) There is a bug in the Gmail filter system. If you try to set up complex Boolean such as

from:[email protected] -to:[email protected] -junk_term

It will work fine on the initial search, but if you use it to create a filter, the terms, especially the NOTs, will be garbled.

The correct syntax, (hat tip to chenghuayang) is to ignore the to's and from's.

[email protected] [email protected] -junk_term