Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching email using Ruby on Rails

I need to fetch email from my gmail account using RoR.

require 'net/pop'
Net::POP3.start('pop.gmail.com', 995, username, password) do |pop|  
    if pop.mails.empty?  
        puts 'No mail.'  
    else  
        #pop.each_mail do |mail|  
            #p mail.header  
            #p mail.pop
            puts "Mails present"
        #end  
     end  
 end  

I get a timeout error.

usr/lib/ruby/1.8/timeout.rb:60:in `new': execution expired
    (Timeout::Error)
    from /usr/lib/ruby/1.8/net/protocol.rb:206:in `old_open'
    from /usr/lib/ruby/1.8/net/protocol.rb:206:in `old_open'
    from /usr/lib/ruby/1.8/net/pop.rb:438:in `do_start'
    from /usr/lib/ruby/1.8/net/pop.rb:432:in `start'
    from script/mail.rb:4

How do I fix that?

like image 252
Shreyas Avatar asked Apr 19 '10 11:04

Shreyas


People also ask

How do I use SMTP in Ruby on Rails?

Go to the config folder of your emails project and open environment. rb file and add the following line at the bottom of this file. It tells ActionMailer that you want to use the SMTP server. You can also set it to be :sendmail if you are using a Unix-based operating system such as Mac OS X or Linux.

How do I Preview mailers in Rails?

Then the preview will be available in http://localhost:3000/rails/mailers/user_mailer/welcome_email. If you change something in app/views/user_mailer/welcome_email. html. erb or the mailer itself, it'll automatically reload and render it so you can visually see the new style instantly.

What is Actionmailbox?

1 What is Action Mailbox? Action Mailbox routes incoming emails to controller-like mailboxes for processing in Rails. It ships with ingresses for Mailgun, Mandrill, Postmark, and SendGrid. You can also handle inbound mails directly via the built-in Exim, Postfix, and Qmail ingresses.


2 Answers

Try this one:

require 'net/pop'
Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)  
Net::POP3.start('pop.gmail.com', 995, username, password) do |pop|  
  if pop.mails.empty?  
    puts 'No mails.'  
  else  
    pop.each_mail do |mail|  
      p mail.header  
      p mail.pop  
    end  
  end  
end  
like image 108
retro Avatar answered Nov 08 '22 07:11

retro


You need to use SSL

like image 30
ryanshackintosh Avatar answered Nov 08 '22 05:11

ryanshackintosh