Gmail filters and Boolean operators

Filed under: neat!,useless @ 13:15

If you haven’t heard me proselytize Gmail before, count yourself lucky; I tend to spout off about it. Yes, I know that Gmail is creepy. I understand all the privacy concerns and the potential to allow Google to earn lots of money off of my correspondence. It’s just so damn convenient.

But I digress.

Gmail allows its users to construct email filters to tag messages with various labels, forward things to different address and so forth by specifying the conditions an email must meet before the filter in question is applied. One can specify that the message be From a particular source, be sent To a certain address, contain specific text and so on. Since the filters are implemented as Gmail search queries, Gmail filters may also include basic boolean logic. For instance, you can create a filter that catches emails From bob@example.com OR sally@example.org. In terms of Gmail’s search syntax, this filter would be denoted as from:(bob@example.com OR sally@example.org). Similarly, NOTs are specified with minus signs, and spaces are used for ANDs.

Gmail’s filters, then, are fairly robust. However, the interface for writing filters is very limiting. The user is presented with five text boxes: From, To, Subject, Has The Words, Doesn’t Have. And that’s all the options we get. Suppose, instead of the above, I wanted to create a filter that caught messages From bob@example.com OR To sally@example.org. I can type ‘bob@example.com’ into the From box and ‘sally@example.org’ into the To box to try to construct such a filter. But Gmail does not offer me the ability to specify the boolean operator that should be applied between the From and To conditions; it assumes AND. I’m hosed.

But only at first glance. You can actually implement a relative OR between fields. A cursory internet search yielded the following clever solution.

In the From field, enter

bob@example.com) OR to:(sally@example.org

It’s the sneaky use of parentheses that makes it all work. Another solution would be to enter the entire search query into the Has The Words field:

from:(bob@example.com) OR to:(sally@example.org)

Though more straightforward, some will argue it isn’t as clean.

Anyway, don’t let Gmail’s (in this case) crappy interface stop you from making arbitrarily complicated filters.


    OG 01.30.2006 @ 14:31

    And let us not forget, gmail let’s you create aliases on the fly. Just add +alias to your gmail address for each website that asks for your e-mail. For example:


    That way, if some site passes your address around in the future, you can automatically filter it to your junk folder.

    Uber 02.01.2006 @ 04:05

    call me a crazy fool, but why not just make different rules for Bob and Alice?

    MDA 02.01.2006 @ 10:33

    OG, Slick trick.

    Uber, Because Gmail only lets you make up to twenty filters. Each one is precious, you crazy fool!

    Anurag 06.02.2006 @ 03:05

    I wish Gmail allowed filtering based on message headers also.

    Paul 08.09.2006 @ 16:08

    I’d love to hear more about those gmail aliases on the fly. are you saying that I can put a plus sign and then any characters after my gmail name, and the message will still come to me? So if I were example@gmail.com, then I could put example+whatever@gmail.com, and it will go to my example@gmail.com account?

    Pretty nifty.

    Loved the boolean logic trip out of the Gmail filtering. I was wondering how I could do that. This is a perfect solution.

    paul.za 08.10.2006 @ 12:01

    The “plussed” address is actually a pretty standard feature of many other email systems — sendmail for example. But if you really want spam-proof email aliases, it’s hard to beat spamgourmet.

    frank 08.18.2006 @ 05:36

    Thanx for the info! really useful.

    There is only one con: the ‘+’ sign isn’t always accepted while entering your plussed adress.

    frank 08.18.2006 @ 05:37

    btw: i had added 38 filters in gmail. Seems like the maximum of 20 filters has been increased?

    Grant 10.09.2006 @ 09:08

    Useful information all around: Complex filters and aliases.

    Thanks to all the contributors.

    Arcady 12.16.2006 @ 15:44

    Thanks, very nifty. I’ve just combined around 20 filters into around 5. :)

    nathan 02.23.2007 @ 17:44

    hey, Here are some corrections, and some handy tips for you. First you can replace the (something) OR (something else) with (without the parentheses) “something | something else” the | acts as the word OR. Little nifty.

    Here are some filters I use.
    create label, bookmarks, than create a filter that is To and from your own email address, that way when you email yourself, It gets labeled bookmarks.

    Create a filter with only has attacthments attatched, and create a label called attacthments, and presso, anything you have attatchmetns with, has attatchments.

    And my favorite filter. In the has words box type: is:spam, say yes to the warning, and than have it sent to trash. I have had only 1 false positive spam, so, this is good, I hate seeing that I have 30 new spams today. I wish though that gmail had a block feature, cause, I would than have the filters apply block to is:spam filter.

    nathan 02.23.2007 @ 17:46

    typo on first paragraph, I meant quotes when I said without the parentheses.

    pleaides 03.06.2007 @ 19:50

    Parentheses can be combined with quotation marks.

    For example the following is also acceptable:

    “sex | drugs | (rock and roll)”

    voyagerfan5761 03.07.2007 @ 16:42

    Sure you can only create 20 filters. How come I have 37?

    Anonymous 07.26.2007 @ 07:46

    i have 120 filters

    Anonymous 08.24.2007 @ 11:55

    Please note that this was originally posted on January 30, 2006 – for all you nitpickers sprouting up a year too late saying “I HAVE 37 FILTERS NOT 20”

    anonymous2 11.02.2007 @ 07:16

    The gmail help says that you can have unlimited filters but only 20 are usable for forwarding.

    Whatever-ishere 11.21.2007 @ 11:05

    thanks for the GREAT post! Very useful…

    gantico 12.11.2007 @ 14:45

    Wow, super thanks for the useful infos.

    Matt 06.05.2008 @ 13:11

    Does anyone know if there is a way to apply the filters to the full message headers of the incoming email?

    Anonymous 08.07.2008 @ 06:24

    Is there a way to apply more than one label to a incoming email?

    Anonymous 08.07.2008 @ 11:50

    you could create more filter rules to apply more than one label.

    Anonymous 08.31.2008 @ 12:07

    Just thought I’d share one of my and most useful favorite filters:

    Email that is both from:me and to:me gets archived AND starred. The “Star” list is then my to-do list, and once I’ve done something, I unstar it (very fast with keyboard shortcuts).

    This is really useful if you have a blackberry or other smart phone. When I see that the lawn needs mowing, I just reach into my pocket, fire off a quick email to myself with subject “mow lawn,” and it will come up in the star list that I check every morning.

    scoob 11.27.2008 @ 18:08

    anyone know how to do this
    from:bob [but nobodyelse!]
    to:me [but nobodyelse!]

    or if thats not possible

    to:me NOT tom NOT dick NOT harry NOT wendy NOT sam

    Basicly I want to filter out individual messages from a malling list I am on. So I can see these personal messages by them selfs.

    scoob 11.27.2008 @ 18:25

    ok I figured it

    to:me -tom -dick -harry -wendy -sam

    Don’t know why ‘NOT’ didn’t work.

    Anywone know if my first example is possible?

    MDA 11.28.2008 @ 01:27

    There is a “list” search parameter you could try playing with.


    You could try something like:

    to:me -list:list@mailinglist.com

    Anonymous 02.20.2010 @ 05:52


    You can create an unlimited number of filters, but only 20 filters can forward to other addresses.

© mdawaffe (Michael D Adams) - Powered by WordPress - Full Credits