Checking and receiving email via ruby. If you need to get emails and check emails via a ruby script this should get you started in the right direction. I needed to copy down about six thousand emails from my pop server, so I wrote up this little script. Ruby makes email simple, you just need to know where to look and unfortunately most searches result in finding a 3rd party library that costs a pretty penny for email actions in ruby. What you really need is the Net::POP3 or the Net::IMAP. After going through the quick documentation and seeing some of the available code examples, writing the code is straightforward. This is just a very simple example, with hardly any error checking. Download the source to Ruby Email Archiver <pre>require ‘net/pop’ Net::POP3.start(‘pop.yourdomain.com’, 110, ‘userName’, ‘myPass’) do pop if pop.mails.empty? puts ‘No mail.’ else i = 0 pop.each_mail do m # or “pop.mails.each …” subject = m.header.split(“\r\n”).grep(/^Subject: /)[0] subject = subject.gsub(“Subject: “,””) subject = subject.gsub(“:”,””) subject = subject[0,10] if(subject.length > 10) File.open(“/archive/#{i}-#{subject}.txt”, ‘w’) do f f.write m.pop end #if you want to delete msg after archive #m.delete i += 1 end puts “#{pop.mails.size} mails popped.” endend</pre>


blog comments powered by Disqus
Dan Mayer Profile Pic
Welcome to Dan Mayer's development blog. I primary write about Ruby development, distributed teams, and dev/PM process. The archives go back to my first CS classes during college when I was first learning programming. I contribute to a few OSS projects and often work on my own projects, You can find my code on github.

Twitter @danmayer

Github @danmayer