Receiving email in your Rails app with Griddler


Posted on

Yesterday I got some really exciting news.  One of my favorite dev shops, thoughtbot, released a gem that will help you receive email in your Ruby on Rails app called Griddler.  The best part?  It’s done using the SendGrid parse API!  In this post, I’ll walk you through integrating it in a sample app in no time.

Sample App Setup

Let’s start with a really simple sample application that displays a list of posts.  We only need one model, Post, and it has a body and email attributes. We could easily scaffold that out with the following command:

$ rails g model post body:text email:string

We also have a Posts controller with an index method and view that lists out all the posts:

app/controllers/posts_controller.rb

app/views/posts/index.html.erb

Setting up Griddler

The first thing we need to do is add Griddler to our dependencies. Add the following line to your Gemfile:

gem 'griddler'

The next thing we need to do is create an EmailProcessor class that will handle all the incoming emails. In this example, I’m going to put it in my models folder because lib isn’t autoloaded by default, but it’s probably not best practice to put it in there. The class will have a process method that will get called with the new email object whenever we receive a mail. Whenever it gets called, we’re going to create a new post with the email’s body as it’s body and the from address as the email.

app/models/email_processor.rb

Setting up the Parse Webhook

Now we have to do is set up a new parse endpoint on SendGrid. To do this, you’ll need to make sure your app is accessible from the web. You could deploy it somewhere like heroku or use the awesome localtunnel gem to expose your local development environment to the web.

You’ll also need a domain with it’s MX record pointing at mx.sendgrid.net. If you don’t know how to do this part, check with your domain’s hosting provider for details.

Head over to the Parse Webhook settings page and create a new record. Your hostname should be your url and the url should point to your app’s email processing endpoint. Griddler adds “/email_processor” as a route on your application. So, if your app is hosted at http://example.com, then the url would be “http://example.com/email_processor”.

sendgrid-parse-ss

Start receiving emails

That’s it! We’ve just integrated incoming email support into our sample application. Now every time you send an email to an email address at your domain, a new post gets created with the email’s body and the from email address.

Of course, just like many of ThoughtBot’s other awesome libraries, Griddler is open source. You can find the source code on Github, so be sure to check it out!


Swift is a developer evangelist at SendGrid who is based out of NYC. He is one of the founders of Hacker League and tweets as @SwiftAlphaOne. Follow him there and check him out at http://theycallmeswift.com.

10 thoughts on “Receiving email in your Rails app with Griddler

  1. I'd expect that,
    `rails g model post body:string email:string`

    would be better written as:
    `rails g model post body:text email:string`

    because several backends impose limits on the length of a string field, often 255 chars. SQLite doesn't care, but PostgreSQL and MySQL do.

  2. Can this also handle attachments to email? Does email body has to be text or it can be HTML and sendgrid will take care of converting it and sending ahead to our application.

  3. You should add a link for this gem in your dev page or parse API documentation. I managed to create my own solution before I found the gem :)

  4. Pingback: Collect Inbound Email Using Python and Flask

  5. Pingback: SendGrid's Parse API: Parsing Incoming Email is Now Faster and Easier

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>