Griddler is Better Than Ever!

Gabe Berke-Williams

Griddler is a Rails engine that provides an endpoint for common email services and hands off the email to your application. You can configure Mailgun (or whatever you’re using) to POST emails to your Rails app to handle incoming email from your users.

Our old Griddler announcement post now has outdated instructions with the changes in Griddler 1.0.0, which means Griddler deserves a new post.

Setup

Let’s walk through setting up Griddler with Mailgun. The instructions are almost exactly the same for other adapters like Mandrill or Sendgrid, though you should read the documentation for each adapter to make sure.

First add the adapter to your Gemfile, which will bring in Griddler with it:

# Gemfile

gem "griddler-mailgun"

The Griddler README has a list of all available adapters.

You must configure Griddler to use your adapter:

# config/initializers/griddler.rb

Griddler.configure do |config|
  config.email_service = :mailgun
end

Routes

Now you need to set up a route that your email service can POST to. You have a few options:

# config/routes.rb

# mount using default path: POST /email_processor
mount_griddler

# or, mount using a custom path: POST /email/incoming
mount_griddler "/email/incoming"

# or, the DIY approach:
post "/email_processor" => "griddler/emails#create"

Once your adapter POSTS to your endpoint, Griddler will pass a Griddler::Email object to a class called EmailProcessor that you create:

class EmailProcessor
  def initialize(email)
    @email = email
  end

  def process
    # all of your code to deal with the email goes here
  end
end

The email object has a lot of attributes, but here are the ones you’ll likely use most:

  • to
  • from
  • subject
  • body
  • raw_body

Configuration

Let’s say you want to configure Griddler to use a Mailman class like this, rather than an EmailProcessor class:

class Mailman
  def initialize(email)
  end

  def handle_it
  end
end

You need to add a few lines to your Griddler initializer:

# config/initializers/griddler.rb

Griddler.configure do |config|
  config.email_service = :mailgun

  config.processor_class = Mailman
  config.processor_method = :handle_it
end

There are more configuration options in the README.

Let your users interact with your app via email

If an email has -- Reply ABOVE THIS LINE -- in it, then Griddler provides everything above that delimiter in the body attribute on the email. If the line isn’t there, then body has everything in the body, as you’d expect.

This lets you grab people’s replies to an email thread without the clutter of past responses, like Basecamp. If you really need the whole body, you can use raw_body.

For more information on Griddler, including how to change the reply delimiter, check out the Griddler website.