views:

227

answers:

2

Task given: An email is stored, byte for byte, in one or more chunks (of fixed length) in a database. This mail is to be retrieved from that database and it's contents shall be displayed to the user.

I have no problem wrapping the legacy database in an ActiveRecord model, concatenating the stored chunks and so on. What I don't really know is where to start on the MIME parsing part. I thought about something like perhaps having a dedicated EMail class which I can initialize with the data stored within the database and that class would allow me to see what MIME parts the mail consist of and allowed me to display, e.g., the text/* parts of it.

Now it seems that ActionMailer is able to parse incoming mails, but the doucmentation on receiving mails seems to be rather, erm, "sparse" and it just mentions receiving mails from STDIN.

How can I parse and display a MIME mail (or parts of it) in Rails, given that I can provide it's contents as a single string, variable, query result or soemthing like that?

A: 

Take a look at MMS2R.

I've been using it lately for parsing emails and it does a jolly good job.

JimNeath
A: 

I did it wrong. Rails comes with the TMail library, which is perfectly capable of parsing MIME emails. The basic workflow is as easy as concatenating the chunks from one stored message and passing them to TMail::Mail.parse like this:

email = TMail::Mail.parse(StoredMessage.find(:all,
  :conditions => ["mail_id = ?", "oyByGqacG73b"],
  :order => "chunk_ind").collect(&:mail_text).join)

email.body #=> this is your test body
email.subject # => test subject
email.has_attachment? #=> true
email.attachments.first.original_filename # => bulkfile

I do really apologize for having missed a whole library in Rails.

cite