views:

32

answers:

1

I am creating a ruby on rails application and I want my users to be able to email the application and have the application take in the email and create a issue record in the database. Is there a GEM or Code that can be used to have the rails application get the email and parse out the body and stick it into an issue table.

A: 

I don't know if there is a gem to accomplish the entire task, but you don't technically need one. I recently did this, and though working with ruby's IMAP library isn't the most fun/intuitive thing in the world, it gets the job done.

IMAP can be used to programmatically access and interact with an email account. Here's an example straight from my code (somwhat obfuscated to be easier for someone to implement):

require 'net/imap'

imap = Net::IMAP.new("imap.gmail.com", 993, true)
imap.login(CONFIG["username"], CONFIG["password"])
imap.select('INBOX')
imap.search(["NOT", "DELETED"]).each do |mail_id|
  mail = TMail::Mail.parse(imap.fetch(mail_id, "RFC822").first.attr["RFC822"])
  do_something_cool(mail)
  imap.store(mail_id, "+FLAGS", :Deleted)
end
imap.expunge
imap.logout()
imap.disconnect()

In this example, I'm accessing a gmail account with the IMAP library, going to the inbox, and grabbing each mail that hasn't been deleted. The TMAIL gem, though not necessary, makes dealing with email much easier. In my case, I need to delete emails after I parse them, so I add a delete flag to the email and then, when I'm done, I clear the account of all deleted emails.

The next half is parsing the email for the data you want and making records out of it. I leave that part to the implementor.

G. Martin