Search code examples
ruby-on-railsruby-on-rails-3gmailgmail-imap

Searching "All Mail" including Inbox and Archived Emails using Gmail gem


I am using Gmail gem to connect to user's Gmail account and search emails:

gmail = Gmail.connect('user', 'pass')
results = gmail.inbox.search(:subject => 'insert_keyword_here')

This returns only the items in Inbox (labeled with Inbox), not the ones in "All Mail" (also known as "archived email").

How can I search "All Mail", not just Inbox?


Solution

  • Perhaps a bit late to the party, but it could be useful for someone for sure...

    If you need to get language-agnostic, you should pick the folder using one of the tags. Here's how to find "all mail" folder:

    mailbox_all_mail = imap.list('', '*').find{|mb| mb.attr.include?(:All)}
    

    And then select (or examine) its name:

    imap.examine(mailbox_all_mail.name)
    

    That should do the trick.