Access Gmail from Ruby using Net::POP3 and Stunnel

Posted on July 20, 2006

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.

Comments

Leave a response

  1. Ali Thu, 20 Jul 2006 11:30:01 UTC

    Very cool! Someday I’ll give Ruby a try. Someday.

  2. Administrator Thu, 20 Jul 2006 17:45:59 UTC

    Bah, just code everything in C. Be a man! Ruby only runs on the Mac that is located inside of a Starbucks.

  3. Niels Sun, 30 Jul 2006 04:19:47 UTC

    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!

  4. Manton Reece Tue, 22 Aug 2006 19:41:10 UTC

    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.

  5. [...] 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. [...]

  6. Mike Tue, 27 Feb 2007 18:51:40 UTC

    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

  7. Mike Sat, 15 Mar 2008 12:34:49 UTC

    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

  8. Damien Mon, 10 Nov 2008 10:01:50 UTC

    You can use gmail with IMAP. And the NET/IMAP plugin is working fine with SSL.
    http://ruby-doc.org/stdlib/libdoc/net/imap/rdoc/classes/Net/IMAP.html

    I guess that solution is easier than the one with stunnel :)

  9. Administrator Mon, 10 Nov 2008 12:32:03 UTC

    Whoa, awesome. Do you have a working example? I tried and tried to get this to work before giving up and opening a stunnel.

  10. Damien Mon, 10 Nov 2008 13:20:26 UTC

    Yeah. The last example given here :
    http://codeclimber.blogspot.com/2008/06/using-ruby-for-imap-with-gmail.html
    is functionnal. I tried it a few hours ago.

  11. raygun Thu, 12 Feb 2009 05:35:25 UTC

    Cool, thanks.

Comments