9

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)?

5 Answers 5

6

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.

4
  • 1
    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
    Aug 16, 2009 at 18:04
  • I use a Google Apps email account. Any account with IMAP access should work, though. Aug 16, 2009 at 22:31
  • 1
    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
    Aug 18, 2009 at 2:40
  • 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
    Aug 21, 2009 at 3:00
4

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

1
  • I haven't tried this one yet, but I'll check it out the next time I face this issue. I've worked with sendgrid.com, and can recommend them to anyone who needs such a service....just wanted to pipe in for future readers!
    – Ryan
    Dec 2, 2010 at 6:30
3

For relaying email from Postfix to your Rails app via HTTP, I just wrote an article on how we're doing it.

It's actually quite simple, but we're also leveraging Postfix's ability to defer email if it can't be relayed. I haven't seen that elsewhere so far. See http://www.doorkeeperhq.com/developer/smtp-to-web-api for details.

1

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.

0

I recently wrote a daemon which receives mail from Postfix using LMTP (it uses EventMachine and is pretty fast and robust) and saves it to MongoDB. In that sense it eliminates the middle man (IMAP server + IMAP client). That approach gives pretty much limitless scalability and redundancy. Other backends support (MySQL, Redis, etc.) can be added in practically no time.

received

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.