IMAP criteria with multiple ORs

白昼怎懂夜的黑 提交于 2019-12-08 20:16:13

问题


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?


回答1:


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




回答2:


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;


来源:https://stackoverflow.com/questions/12809709/imap-criteria-with-multiple-ors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!