Access Gmail from Ruby using Net::POP3 and Stunnel
I Googled around for this at one point, however, all I could find were ill-fated attempts by a couple of tortured souls on ruby-talk. Google offers POP3S access to Gmail, but, unfortunately, Ruby’s Net::POP3S module is still under development. POP3S is the much reviled and revered Post Office Protocol tunneled over a thick blanket of SSL obfuscation. Fortunately, the good Stunnel folks have anticipated this kind of thing and they’re giving out some code that can encode and decode SSL to a standard socket.
This was done on OS X, but Stunnel seems to work on most platforms. However, I wasn’t finding it in ports or fink. So, I had to roll my own stunnel binary.
wget http://www.stunnel.org/download/stunnel/src/stunnel-3.22.tar.gz tar zxf stunnel-3.22.tar.gz cd stunnel-3.22 ./configure make sudo make install
Whew. So, after you’ve installed stunnel, you need to invoke it to redirect a local port to Google’s secure POP3 port located at pop.gmail.com on port 995. You can redirect any local port to Google’s pop server. I chose to use port 42 because that’s as good as anything else. Here’s my stunnel invocation.
sudo /usr/local/sbin/stunnel -c -d 42 -r pop.gmail.com:995
So, now you can use Ruby’s Net::POP3 to access your gmail from port 42 on your local machine. Stunnel will handle the details of SSL.
require 'net/pop'
pop = Net::POP3.new('localhost', 42)
pop.start('mrbeastly', 'sUp3Rs3kRiT!')
if pop.mails.empty?
puts 'No mail.'
else
i = 0
pop.each_mail do |m|
exit if i > 20
puts m.pop
i=i+1
end
end
Running this code will display the first 20 emails in Gmail to your terminal. This is really a stopgap solution until the fabled POP3S patch gets accepted into Ruby’s Net library. This is rumored to be soon. So, I’ll keep my hopes up.
Trackbacks
Use this link to trackback from your own site.






Very cool! Someday I’ll give Ruby a try. Someday.
Bah, just code everything in C. Be a man! Ruby only runs on the Mac that is located inside of a Starbucks.
Thank you for this, this is handy.
Ruby is a great language, i enjoy thinking logically instead of typing too many lines to get something on the web (ahem… yeah. dont to web programming in C please, its possible but its just not necessary.)
So i can finally get a GMail connection, thats sweet.
keep up the posts!
Excellent, this comes along at a convenient time for me as I just restarted work on a project that uses Net::POP3. I’ll definitely give Stunnel a try.
[...] You see, gmail only accepts POP3 connections over SSL (on port 995), a feature not found in our beloved net/pop library. Hmm. Time for some help from google. The first page I came across described using stunnel to create an ssl tunnel to route standard net/pop traffic through. This sounded great, except that I couldn’t get stunnel (v4.16) to compile on mac os x, although I didn’t try too hard… The other point of interest in that original article was that the pop3 with ssl support was in development. The next step was to track down some mailing list threads that talked about the ssl support and hope for a patch. We’re in luck, the first patch was actually submitted in March 2004, with the second, accepted and committed, patch coming in April 2004. As per the last message in the second thread, the patch was accepted into core, and is now available in the 1.9 release. So, we don’t even need to patch our existing copy of net/pop, we can just download the newer version. I named this downloaded version as pop_ssl, to avoid confusion, and placed it in my ruby load path. [...]
hey this was a big help to me. Here’s the config file I use for the newest version of Stunnel:
[gmail]
client = yes
accept = 42
connect = pop.gmail.com:995
delay = yes
I run stunnel as a service which use that file as a config
This was a great help to me. I needed to be able to connect to arbitrary POP servers, not just gmail, so I figured out to spawn stunnels on the fly. In case anyone else needs it:
http://www.subelsky.com/2008/03/using-stunnel-to-wrap-ruby-network.html