---
title: Handle incoming email with Griddler
teaser: Griddler is a new open source library to handle inbound emails.
tags: news,web,rails,griddler
author: Joel Oliveira
published_on: 2013-02-04
---

![Griddler](https://images.thoughtbot.com/handle-incoming-email-with-griddler/HXu4vCvbTLGjzKmykaLH_tumblr_inline_mh91t07g2s1qz4rgp.jpg)

FYI: _We have updated Griddler instructions in [Griddler is Better Than Ever!]_

For all the likes, shares, tweets, pokes, follows, and friends, there's a
fundamental core to the internet that, no matter how hard some might hope, will
never go away—email. Rails has built-in support for outgoing mail with
ActionMailer, but nothing on the _omakase_ menu handles incoming mail. To help
with that, we extracted [Griddler](https://github.com/thoughtbot/griddler) from
[Trajectory](https://www.apptrajectory.com) and are now happy to release
it&mdash;hot off the... ahem... presses.

Griddler is a Rails engine that provides an endpoint for the SendGrid Parse API.
It hands off a preprocessed email object to a class implemented by you. We're
happy to look at pull requests that interface with other email services.

## Setup

To get Griddler integrated with your app, add Griddler to your `Gemfile`, and
`bundle` away:

```ruby
gem 'griddler'
```

Griddler automatically adds an endpoint to your routes table resembling the
following:

```ruby
post '/email_processor' => 'griddler/emails#create'
```

But you may copy, paste, and modify that anywhere else in your routes for the
purposes of your application.

Once Sendgrid posts to your endpoint Griddler will take care of packaging up the
important bits of that data and providing a nice `Griddler::Email` object for
you. The contract we expect you to go in on with Griddler at this point is that
you will implement a class called `EmailProcessor`, containing a class method
called `process`, which we will be passing that packaged up instance of
`Griddler::Email` into.

For example, in `lib/email_processor.rb`:

```ruby
class EmailProcessor
  def self.process(email)
    # all of your application-specific code here - creating models,
    # processing reports, etc
  end
end
```

The email object contains the following attributes:

* to
* from
* subject
* body
* raw_body

Of those, `to`, `from`, and `subject` fall on the obvious side as to their
purpose.

## Let your users interact with your app via email

What isn't entirely obvious (but very cool) is that Griddler helps you handle
the email body by cleaning up replies and providing the important parts of an
email before `-- Reply ABOVE THIS LINE --` in the `body` attribute. Note that
the reply delimiter is adjustable in the configuration. We keep `raw_body`
around, as contains everything before Griddler scrubs it into `body` so that you
may use the contents for other purposes.

There is much more information in the [Griddler README] explaining the details,
configuration, testing, and other bits.

If you like it, let us know what you think! As always, you can [find the code on
GitHub][github]. We look forward to hearing all of the ways you use Griddler!

[Griddler is Better Than Ever!]: https://thoughtbot.com/blog/griddler-is-better-than-ever
[Griddler README]: https://github.com/thoughtbot/griddler/blob/master/README.md
[github]: https://github.com/thoughtbot/griddler
