2

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 a1@b.com SINCE 1-Oct-2018) (OR (FROM a2@b.com SINCE 10-Oct-2018) (OR (FROM a3@b.com 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.

sawa
  • 165,429
  • 45
  • 277
  • 381
willyab
  • 167
  • 1
  • 10

1 Answers1

4

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 a3@b.com SINCE 19-Oct-2018)).

What you mean is probably OR (FROM a1@b.com SINCE 1-Oct-2018) OR (FROM a2@b.com SINCE 10-Oct-2018) (FROM a3@b.com 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.)

arnt
  • 8,949
  • 5
  • 24
  • 32
  • imho, the `OR` syntax in IMAP is not so great. Everybody (and a lot of server implementations) get it wrong. – Max Nov 29 '18 at 16:54
  • Agree. Search syntax is a bit spotty in general and OR is perhaps the worst. – arnt Nov 30 '18 at 11:15
  • 1
    The easiest way to do this is to build an expression tree and then serialize it down. – jstedfast Nov 30 '18 at 12:45