Receiving Email with Ruby (and Rails)

Posted by Luke Francl
on Wednesday, November 05

Mailbox photo by a4gpaEarlier this year, Mike Mondragon and I co-authored a PeepCode book about MMS2R. But it was never really about MMS2R, it was about the broader topic of receiving and processing email with Ruby and Rails—and MMS2R is a key library for this. MMS2R makes processing multipart emails a snap. It does all the heavy lifting for you. The rest of the book explains the ins and outs of processing email with Rails, including best practices for matching users to the emails they submit.

Because of that, the book’s been renamed. It’s now called Receiving Email with Ruby.

Everyone I’ve talked to about the book has said that it’s been very helpful for them. If you’re looking at integrating email into your application, this is the place to start. Learn from Mike and my mistakes to your benefit! At only $9, it is a steal. Buy your copy today! :)

Photo by a4gpa.

Comments

Leave a response

  1. carlos@webbynodeNovember 06, 2008 @ 03:09 PM

    Hey there. We’re trying to accomplish this with a new app we’re building for our server management. This is, we already have a current email server, is there any way it can tie up to it?

    Is it covered in the book?

  2. Luke FranclNovember 06, 2008 @ 05:48 PM

    carlos,

    Probably. Does your mail server speak POP3 or IMAP? That’s what the Fetcher plugin supports. That’s what the book shows.

    If not, if you can write a module that can download mail from the server, that’s basically what you need to do. The book shows how to run a process that checks for mail periodically (either as a daemon or a cron job).

  3. Scott MotteNovember 19, 2008 @ 05:26 PM

    Hi Luke,

    I got your guys peepcode, and enjoyed it. In an afternoon I was able to get receiving of emails all setup. Thanks!

    However, I was wondering how you dealt with the issue of the quoted text in a reply. I would like to strip out the quoted text, but since each mail client is different in its formatting of the quoted text I’ve thrown out the possibility of using a regular expression.

    These guys solution (http://www.onthecity.org/) was to force the user to type the text between some stars, but that is unnatural and could be a bit unwieldy for some users.

    What did you guys do or what would you do?

  4. Luke FranclNovember 20, 2008 @ 06:11 PM

    Hi Scott,

    Thanks for buying the book! I’m glad you liked it.

    I’ve never had a need for dealing with quoting in emails because I have been mostly interested in what people are attaching to the emails. It seems like a hard problem because there are so many ways to quote in email, and it depends if you’re talking about plain-text email or HTML email.

    I guess I’d look for a library or some code that does this. A lot of mailing list web software is able to show quoted replies with appropriate formatting, so maybe that’s a place to look?

    Let us know what you find out…

  5. malkomalkoNovember 22, 2008 @ 05:15 PM

    I’ve been looking into how to remove quoted text as well. I think I may’ve come up with a hack that works pretty well. It only works for email providers that start the quoted text with ‘On Sat Nov.. etc etc’.

    Most providers start it off with that On date line.

    incoming_mail = MMS2R::Media.new(email) incoming_mail.body.reverse.split(’ nO’)[-1].reverse

    Basically, I reverse the body string and then split it at ’ nO’ which will very rarely appear in a body as it’s case sensitive. I then get the last element of the Array that is returned, and then reverse it again.

    Thoughts?

  6. Scott MotteNovember 25, 2008 @ 06:01 PM

    MalkoMalko. Thank you. That is extremely clever, and did the trick. I combined with my strategy to handle most emails sent from outlook.

    mail.body.split(‘-Original Message-‘)[0].reverse.split(‘nO’)[-1].reverse