6

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 "somebody@email.com") (TO "somebody@email.com")'

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 "somebodyelse@email.com") (TO "somebodyelse@email.com")) ( OR (FROM "somebody@email.com") (TO "somebody@email.com"))'

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?

forste
  • 1,103
  • 3
  • 14
  • 33
  • Try without spaces between ( and OR – Max Oct 10 '12 at 01:56
  • Did you ever get this to work? – kkurian Oct 14 '12 at 05:32
  • @kkurian 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 here – forste Oct 15 '12 at 00:24

3 Answers3

7

Do not use parenthesis.

IMAP uses polish notation which does not need them:

UID SEARCH OR OR FROM one@mail.com TO one@mail.com OR FROM two@mail.com TO two@mail.com

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

Pawel Lesnikowski
  • 6,264
  • 4
  • 38
  • 42
  • 1
    Is the Polish notation mentioned on the rfc document? – George Daramouskas Jun 16 '16 at 13:02
  • 1
    It's not mentioned explicitly, rather entire SEARCH grammar is defined on page 89 (RFC is here: https://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. – Pawel Lesnikowski Sep 09 '16 at 18:16
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;
Waxrat
  • 2,075
  • 15
  • 13
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 "somebodyelse@email.com" TO "somebodyelse@email.com"

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

UID SEARCH OR OR FROM "somebodyelse@email.com" TO "somebodyelse@email.com" FROM "somebody@email.com"

e.g., 4 operands

UID SEARCH OR OR OR FROM "somebodyelse@email.com" TO "somebodyelse@email.com" FROM "somebody@email.com" TO "somebody@email.com"

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.

healthybodhi
  • 306
  • 2
  • 4