IMAP criteria with multiple ORs
Asked Answered
E

3

7

I am using gmail's IMAP API to search for mails.

I use the 'OR' criteria to search for different keywords. This works well if I go one level only, i.e. something like

'UID SEARCH OR (FROM "[email protected]") (TO "[email protected]")'

however, it does not work when I try a longer expression like

criteria OR criteria OR criteria or criteria

which translates to (as far as I understand the syntax)

'UID SEARCH OR ( OR (FROM "[email protected]") (TO "[email protected]")) ( OR (FROM "[email protected]") (TO "[email protected]"))'

to make it clear I basically want all messages that are either sent from or sent to ANY of a given list of emails

The error I get from the libray is

[ 'A34', 'BAD', 'Could', 'not', 'parse', 'command' ]

any suggestions?

Egest answered 9/10, 2012 at 23:8 Comment(3)
Try without spaces between ( and ORJocelynjocelyne
Did you ever get this to work?Anabiosis
@Anabiosis not yet, I worked around it by calling for each statement separately and then concatenating the lists. I am using a node.js IMAP lib which I would have to rewrite to follow max advice. will do that later probably and post results hereEgest
B
7

Do not use parenthesis.

IMAP uses polish notation which does not need them:

UID SEARCH OR OR FROM [email protected] TO [email protected] OR FROM [email protected] TO [email protected]

There are cases where parenthesis are needed (as IMAP AND operator can take more than 2 operands): https://www.limilabs.com/blog/imap-search-requires-parentheses

Brey answered 23/10, 2012 at 12:42 Comment(2)
Is the Polish notation mentioned on the rfc document?Paleobiology
It's not mentioned explicitly, rather entire SEARCH grammar is defined on page 89 (RFC is here: tools.ietf.org/html/rfc3501#page-89). There is also no explicitly defined "AND" operator, which makes parenthesis required for some queries. I described that in the blog post in detail.Brey
B
4

So if you have N expressions that you want to "OR" together, you would prefix those N expressions by N-1 "OR"s.

In Perl, for example, that would be:

$search = join " ", ("OR") x (@exprs - 1), @exprs;
Ballroom answered 13/2, 2016 at 14:34 Comment(0)
O
1

Just to clarify Pawel's answer (posting since I lack the points to comment), as I didn't quite pick up the nuance:

OR is a prefix operator and can only take two operands (not more). So this translates into:

UID SEARCH OR FROM "[email protected]" TO "[email protected]"

If you have more than two, you need to add an OR: e.g., 3 operands

UID SEARCH OR OR FROM "[email protected]" TO "[email protected]" FROM "[email protected]"

e.g., 4 operands

UID SEARCH OR OR OR FROM "[email protected]" TO "[email protected]" FROM "[email protected]" TO "[email protected]"

In other words, for four operands either: Pawel's answer of :

OR (OR x x) (OR x x)

or what I outlined above:

(OR (OR (OR x x) x) x)

Should work.

Occult answered 28/7, 2022 at 3:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.