Search code examples
ruby-on-railsrubyimapgmail-imap

IMAP + Ruby: How to search emails with multiple parameters with optional conditioning


I have code using which I am able to fetch emails from authenticated gmail account. At the moment there is a basic filter applied on it. But my requirement needs me to apply multiple filters like:

[from: space@nasa.com]
AND [subject: life on mars]
OR [subject: life on venus]
AND [subject "not": life on earth]

Can any when suggest how I can apply filters like this on imap.search command?


Solution

  • You can try

    imap.search(["FROM", "space@nasa.com", "SUBJECT", "life on mars", "OR", "life on venus", "NOT", "life on earth"])
    

    Full documentation is available here: http://www.ruby-doc.org/stdlib-2.0/libdoc/net/imap/rdoc/Net/IMAP.html