willyab
willyab

Reputation: 325

imap search syntax with multiple OR arguments

I am using Ruby 2.5.3 & the mail gem (2.7.1). I am structuring the IMAP search command to retrieve emails given a list of email addresses and various since dates. It is a logical OR of the search email addresses.

I am using this email_filter:

(OR (FROM [email protected] SINCE 1-Oct-2018) (OR (FROM [email protected] SINCE 10-Oct-2018) (OR (FROM [email protected] SINCE 19-Oct-2018))))

which seems to be consistent with the RFC 3501 ABNR form.

The ruby code: to structure the search:

search_options = { count: no_emails_to_process, what: :first, order: :asc, keys: email_filter}
Mail.find(search_options) do |mail, imap, uid, attrs| 
  etc ...

It raised an error:

Error in IMAP command UID SEARCH: Missing argument

I assume the syntax isn't right because limiting the search to just one email address works fine.

I need some help.

Upvotes: 2

Views: 2031

Answers (1)

arnt
arnt

Reputation: 9675

OR takes two arguments, neither more nor less. OR a b works, (OR a b) works but(OR a) won't work. That would be a single-argument OR inside a single-argument AND. The parser is looking for the second argument to OR when it runs up against the ) that ends the list of arguments to AND. The last part of your query is (OR (FROM [email protected] SINCE 19-Oct-2018)).

What you mean is probably OR (FROM [email protected] SINCE 1-Oct-2018) OR (FROM [email protected] SINCE 10-Oct-2018) (FROM [email protected] SINCE 19-Oct-2018). In that expression, the first OR takes two arguments, which are an AND and another OR, and the second OR takes two arguments, both of which are ANDs.

(I agree that this difference between OR and AND is a bit strange.)

Upvotes: 4

Related Questions