20 May 2007

Ruby email to rss

Dan Mayer
Dan Mayer @danmayer
A simple ruby email2rss example When I was searching for some basic ruby scripts to download pop email I came across the email 2 Rss project on Ruby forge. Since I noticed there was no code available for the project yet I figured I would make a quick version up and make the code available, so anyone else searching for similar examples would have a starting point. This code is just a functional example, but doesn’t have any real options or error checking so use at your own risk. Download the source to Ruby Email to RSS Feed <pre>require ‘net/pop’require ‘rss/maker’ version = “2.0” # [“0.9”, “1.0”, “2.0”]destination = “/archive/emailFeed.xml” # local file to write #create a new email Feedcontent = RSS::Maker.make(version) do rss rss.channel.title = “Dan Email Feed” rss.channel.link = “http://www.pretheory.com” rss.channel.description = “My newest emails” rss.items.do_sort = true # sort items by date 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,15] if(subject.length > 15) link = ‘http://www.gmail.com’ body = m.pop body = body[0,25] if(body.length > 25) #create feed item item = rss.items.new_item item.title = subject item.link = link item.date = Time.now item.description = body i += 1 end puts “#{pop.mails.size} mails popped.” end endend File.open(destination,”w”) do f f.write(content)end puts ‘Done. Thanks.’</pre>

Categories