Sunday, February 17, 2008

Mail Fetcher - Fetch Email from Ruby and Rails

I recently had to write something to fetch emails from a GMail account and process the emails to check if the email was a bounce (bad email address). If so, the person in the system associated with the bounced email would be deleted from the database. I did some searching for such thing and found this:

How to receive emails with Rails

About half way down the page you see an example for polling emails from a POP3 server (edited for space):

require 'net/pop'
require File.dirname(__FILE__) + '/../config/environment'

logger = RAILS_DEFAULT_LOGGER

logger.info "Running Mail Importer..."
Net::POP3.start("mail.server.net", nil, "username", "password") do |pop|
if pop.mails.empty?
logger.info "NO MAIL"
else
pop.mails.each do |email|

begin
logger.info "receiving mail..."
Mailman.receive(TMail::Mail.parse(email.pop))
email.delete
rescue Exception => e
logger.error e.message
end

end
end
end
logger.info "Finished Mail Importer."

So try this with a GMail server and you get no connection. Why? Because Google uses SSL when logging in via POP3. So the I looked around and found someone using SSL to login via POP3. All they did was add this line right before trying to start the connection: Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)

Simple enough. I went ahead and tried that, and it blows up with the following error: undefined method `enable_ssl' for Net::POP3:Class. Now trying to find out why this didn't work was annoying, but I finally found out that in the current version of Ruby (1.8.6), Net::POP3 doesn't understand enable_ssl. The edge version, however, does. So, I got the net/pop.rb file and required it instead of the 1.8.6 version and voila! It works! So, instead of just posting this and letting everyone know that you have to do this to make SSL POP3 access work, I made a plugin:

Mail Fetcher

Now the whole world may enjoy SSL POP3 access to their hearts' content. And, it also works with IMAP. Checkout the README and mail_fetcher.rb to get all the details. Enjoy!

15 comments:

Zendesk said...
This comment has been removed by the author.
Anonymous said...

This is what we use:

http://railspikes.com/2007/6/1/rails-email-processing
http://slantwisedesign.com/rdoc/fetcher/

It reads a mail.yml file in which you can put ssl: true

Anonymous said...

Thanks that was really useful. Keep up the excellent work

Josh Lippiner said...

Ok - This is a great plugin, however, for some reason the POP delete does not seem to be working.

While it appears to delete it (somehow) in the sense that if I try to fetch again from Google it wont, when I log into Google the message is still there?

Any thoughts?

Anonymous said...

best registry cleaner , A utility software that tests the Pc registry and looks for outdated and ill items.
Apps usually create Registry entries for momentary data and pointers along with other files, but in no way remove them. Furthermore, http://www.registry1000.com/2010/05/09/best-registry-cleaner/ , [url=best registry cleaner]http://www.registry1000.com/2010/05/09/best-registry-cleaner/[/url] if ringbinders are personally deleted by the user.
Registry entries may point to files that don't exist. A Registry scanner can ahtvniwxnjnjnjnnjaw be part of a arrangement of method programs or even a stand-alone, best registry cleaner system

Anonymous said...

Thanks for sharing this link, but unfortunately it seems to be down... Does anybody have a mirror or another source? Please answer to my post if you do!

I would appreciate if a staff member here at siannopollo.blogspot.com could post it.

Thanks,
Harry

Anonymous said...

Have you considered the fact that this might work another way? I am wondering if anyone else has come across something
like this in the past? Let me know your thoughts...

Anonymous said...

Excellent blog! I genuinely man how it' s acquiescent on my eyes as ably as the info are warmly written. I am wondering how I may be notified whenever a stylish record has been made. I suffer with subscribed to your rss maintain which should do the art! Get a punctilious age!
[IMG]http://www.sedonarapidweightloss.com/weightloss-diet/34/b/happy.gif[/IMG]

Mike said...

There is tool named as Digeus I recommend it when there are problems with windows system. I also recommend to use Tune Up Suite. It helps to eliminate system crashed.

Anonymous said...

I really liked your article. cardiovascular Read a useful article about tramadol tramadol

Anonymous said...

This is my fiгst time раy а quicκ viѕit at here and i am
гeаlly happy to reaԁ all at onе plaсe.


Feel fгеe to surf to mу web blog - http://freemacway.com/v2-Cigs-Coupon-codes/
My homepage ; v2 cigs coupon code 2012

Anonymous said...

Hello. Facebook takes a [url=http://www.casinobonus.gd]online casinos[/url] wager move in reverse on 888 casino grapple with: Facebook is expanding its efforts to launch real-money gaming to millions of British users after announcing a seize with with the online gambling companions 888 Holdings.And Bye.

Anonymous said...

top [url=http://www.001casino.com/]casino online[/url] brake the latest [url=http://www.casinolasvegass.com/]free casino bonus[/url] autonomous no deposit bonus at the foremost [url=http://www.baywatchcasino.com/]online casino
[/url].

Anonymous said...

There are a variety of motives why people today choose this belt.


my web blog: the Flex belt Reviews

Anonymous said...

Giving the respond to of this concern is by some means complicated and not easier like this problem.
This scan resource package deal prices around $150.


my blog - obd2 scan tool