views:

686

answers:

3

Hey Everyone,

I've been trying to figure out the best way to handle incoming email in a rails applications. I realize "best practices" is quite subjective, so I'll start by stating that my primary concerns are scalability and efficiency. This is an issue primarily because my use will involve handling potentially large attachments.

Seems like just yesterday the accepted method was to use ActionMailer to receive the email, but recently I've stumbled across several articles saying this is inefficient as it spawns a new rails instance with each email (horrible at high volumes).

Most recently, this article has been getting my attention: http://jasonseifer.com/2009/04/24/receving-email-with-rails

The post talks about a slimmed down version of the ActionMailer system that isn't forced to spawn an entire rails instance, but the comments talk about several other options like a dedicated mail directory (maildir?) and imap/pop retrieval.

My question is: Does anyone have any thoughts on what the best option would currently be for processing incoming email in a rails application (including attachments)?

+1  A: 

I maintain the Fetcher plugin for download email from an IMAP server which I use with cron. I used to use a daemon but that was tough to keep running (even with monit) because Ruby would hang. Cron is OK for my workload but it does spawn a Rails process once a minute.

For processing attachments, check out the MMS2R library. It has a nice interface for getting the files out of an email.

The other approach I've had recommended to me is to fire off an HTTP post for each message received. Then you can scale your web tier to handle it.

Shameless plug: you may want to check out Mike Mondragon and my PeepCode book on receiving email with Ruby.

Luke Francl
Shameless plugs aren't shameless if they're relevant ;-), I'll check it out. with regard to the IMAP option, how does that work exactly? Do you need to have access to an external email account elsewhere, or do you host your own email server and just pull from that? Thanks!
Ryan
I use a Google Apps email account. Any account with IMAP access should work, though.
Luke Francl
For my particular case I'm going to use http post just because my situation isn't conducive to IMAP access (email for an entire domain). Thanks for the answer, and thanks for turning me on to MMS2R!
Ryan
Any ideas on how to process multiple attachments with mms2r. been working on an mms2r => paperclip solution for a while now. it works with default_media, but not with media['image/jpeg'].....
Ryan
+1  A: 

in mms2r the MMS2R#default_media just returns the largest video attachment if it exists, or the largest image attachment if it exists, or the largest text attachment if it exists, in that order. MMS2R#default_text returns the largest text/plain attachment if it exists. You can also access any of the attachments directly through mms2r's media hash, e.g. MMS2R#media. MMS2R#media is keyed by mimetype, that value referenced by the key is an array of media of that type. So if there were two jpeg attachements in an email processed by MMS2R, you would access them as an array by keying the media hash with image/jpeg, e.g. MMS2R#media['image/jpeg']

When MMS2R first processes an email, any attachment it finds is decoded and stored in an array of media of that type. As I said, that array is then keyed by mimetype in the MMS2R#media hash.

Buy the book, I need to pay my mortgage PeepCode book on receiving email with Ruby.

monde
A: 

You could try to use a service like http://cloudmailin.com/

MatthewFord