Andrew
Andrew

Reputation: 238937

Parsing Email with Ruby: unexpected token ATOM (expected SPACE) (Net::IMAP::ResponseParseError)

I am getting started parsing email with Ruby. I'm trying to read from my GMail account:

require 'rubygems'
require 'mail'

Mail.defaults do
  retriever_method :imap, :address    => "imap.gmail.com",
                          :port       => 995,
                          :user_name  => '[email protected]',
                          :password   => 'password',
                          :enable_ssl => true
end

emails = Mail.all

emails.each do |email|
    puts email.inspect
end

...but I'm getting this error:

/Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:3277:in `parse_error': unexpected token ATOM (expected SPACE) (Net::IMAP::ResponseParseError)
    from /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:3129:in `match'
    from /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:2100:in `continue_req'
    from /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:2087:in `response'
    from /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:2015:in `parse'
    from /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:1166:in `get_response'
    from /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:1051:in `initialize'
    from /Users/andrew/.rvm/gems/ruby-1.9.3-p194/gems/mail-2.4.4/lib/mail/network/retriever_methods/imap.rb:143:in `new'
    from /Users/andrew/.rvm/gems/ruby-1.9.3-p194/gems/mail-2.4.4/lib/mail/network/retriever_methods/imap.rb:143:in `start'
    from /Users/andrew/.rvm/gems/ruby-1.9.3-p194/gems/mail-2.4.4/lib/mail/network/retriever_methods/imap.rb:65:in `find'
    from /Users/andrew/.rvm/gems/ruby-1.9.3-p194/gems/mail-2.4.4/lib/mail/network/retriever_methods/base.rb:41:in `all'
    from /Users/andrew/.rvm/gems/ruby-1.9.3-p194/gems/mail-2.4.4/lib/mail/mail.rb:171:in `all'

Upvotes: 1

Views: 2086

Answers (2)

bhaak
bhaak

Reputation: 71

This error message is typical if you try to do IMAP on a POP3 mail server.

Google Mail's IMAP port is 993. The port you tried is for POP3.

Upvotes: 1

joelparkerhenderson
joelparkerhenderson

Reputation: 35483

Ruby's IMAP parser has had bugs. Your error output shows that the problem isn't likely to be in your code, it's likely to be the Ruby IMAP parser code that you can't easily change without patching Ruby.

If you're just interested in Gmail, and want to try a easier solution, try the Gmail gem:

https://github.com/nu7hatch/gmail

If you're interested in knowing the details of what's happening and possily how to patch Ruby:

http://claudiofloreani.blogspot.com/2012/01/monkeypatching-ruby-imap-class-to-build.html

Upvotes: 3

Related Questions