« Ruby Email Archiver | Main | Slow down »

Ruby email to rss

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

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 Feed
content = 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
end
end

File.open(destination,"w") do |f|
f.write(content)
end

puts 'Done. Thanks.'

Comments

Web 2.0 craziness

View Dan Mayer's profile on LinkedIn

I Power Seekler
I Power Seekler




www.flickr.com
This is a Flickr badge showing public photos and videos from mayer_dan. Make your own badge here.