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.
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
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
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
Let’s say you want to configure Griddler to use a
Mailman class like this,
rather than an
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.
If an email has
-- Reply ABOVE THIS LINE -- in it, then Griddler provides
everything above that delimiter in the
body attribute on the
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
For more information on Griddler, including how to change the reply delimiter, check out the Griddler website.