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.

Another Step Toward the Triumverate of Email Promulence

Posted on December 07, 2005

I managed to solve one of the three problems that I talked about earlier by using teh G00gle. Apparently, you can use this script to mirror email to Gmail from a Berkeley format mail file. The command-line invocation is highly-bizarre, but it seemed to work on two out of the three mail files that I tried. So, I just mirrored all of my email from 1999 to 2001 to Gmail as a test, and it worked. Now, I’ll have a completely searchable mail archive indexed automagically. This had the by-product of syncing all of my old email to my Powerbook as well. So, now everything is anywhere I could possibly need it, backed up, and highly accessible. Before I put all of my old email in GMail have to go in with Mutt and manually find everything on a per-folder basis. I still have two problems left to solve: (1) Forwarding all sent mail from Mail.app to Gmail automatically using sendmail configuration foo, and (2) Getting all sent Gmail on to hudge.

Reaching Valhalla via Port 25 (over SSH)

Posted on December 06, 2005

I was infinitely proud of this last hack on OS X that seems to work. I wanted to send email from an arbitrary wireless or wired IP address from Mail.app. So, here’s what I did… I issued this command to route port 25 from suudsu to hudge!

sudo ssh -L 25:localhost:25 hoyhoy@hudge

Hudge is where involution.com is hosted and runs a mail server. So, I redirect my Powerbook’s port 25 to hudge, and configure Mail.app to use localhost as it’s outgoing sendmail server over an encrypted link via SSH I’m syncing my Powerbook to Gmail’s POP access (over SSL). Involution.com’s mail (*@involution.com) that is not stopped by Spamassassin, Bogofilter, DCC, or my custom virus notification and unreadable character set filters is forwarded to GMail if it’s not something that was sent from within gmail, and mails sent to hoyhoy@example.com are forwarded back to hudge which implicitly creates a backup. Also, I setup Mutt to BCC sent mail to GMail so it’s included in the archive. The only one that I haven’t figured out is how to make Mail.app BCC sent mail to Gmail, and how to make GMail send it’s sent mail back to hudge. I know there’s ample sendmail foo to be messed with, and I imagine it’s possible to replicate based on the fact that it knows when an email is coming from the Mail.app MUA and tony@involution.com, it is safe to mirror that to my user account on hudge. Similarly, I think I can actually hack Eric S. Raymond’s “fetchmail” program to download emails that I sent from GMail over POP3, and only grab any email that I didn’t send to myself, and archive it on hudge. These are automatically in Mail.app because it syncs to GMail POP3, and sent email is part of what’s downloaded. So, basically, I’m left with emails I sent from Mail.app only being in Mail.app, and email sent from GMail only going to Mail.app and not hudge, but everything else is mirrored everywhere. I used to keep Outlook running on Windows too, but I’ve since stopped being part of that whole scene about four years ago. I transferred all of my Outlook email over to Berkely format and use pine to look through it, on the rare occassion I need an email from before 2002. If only I could somehow get all of that imported to GMail, but that’s another story. Someone needs to write a GMailFS driver to where I can simply drag .eml files or entire mbx files and have them import.

Total Gmail Integration

Posted on December 05, 2005

I don’t know why I feel the need to have the most complicated mail setup in the world, but everyday it seems like I’m adding another layer of complexity. I setup a Gmail account in 2004, and I’ve been using it more and more, however it’s introduced a lot of complexity into how I deal with email. I used to keep everything on one machine in the Berkeley mail format. It was easier back then, but when my network would go down, I’d be unable to check my mail, and not every machine has access to ssh. So, I started using Gmail, and setup a procmail rule to forward all of my involution.com mail to Gmail. The problem with that was, I wasn’t getting my gmail on my home machine. So, I setup Gmail to forward all the email that I got back to my mail server, and prevented it from forwarding that back (an infinite loop). Then, I have the problem of email that I send from mutt doesn’t get forwarded to gmail. So, then I hacked it to append a BCC header to forward sent items on to my Gmail account. Now, the latest conundrum is, how do I make Gmail BCC sent-items back to my server? I haven’t figured that one out.