Handle Inbound Email in any Rack Application

We built Incoming for Honeybadger to streamline discussions about errors. For example, you can comment on any error just by replying to the notification. Most of our comments come in this way, so we consider it a big win.

So your client just called...

He wants his app to handle incoming email. Just like Basecamp.

No problem, you think, lots of services will send you email by POST to your app. Should be easy. Right?

Nope.

Each service uses a proprietary format. Some send you the parsed email, some send it raw. Some send an authentication signature. Some don't.

Wouldn't it be nice if you could skip this part, and work with standard (well-known) ruby objects?

Wouldn't it be nice if you could change vendors...without rewriting your code?

If you're using Incoming!, you can.

TL;DR

Incoming! takes a Rack::Request and gives you a Mail::Message

Supported Services

Incoming supports these great services:

  1. SendGrid

  2. Mailgun

  3. Postmark

  4. CloudMailin

  5. Actual mail servers you run yourself (woah)

Best of all, it's easy to extend. So it's already compatible with services that don't even exist yet! (not really, but kind of)

So why do I want inbound email?

Giving people an option to do something by sending you an email makes them more likely to do it.

We built Incoming! for honeybadger.io. We needed a way to streamline discussions about errors. For example, you can comment on any error just by replying to the notification. Most of our comments come in this way, so we consider it a big win.

As of this writing, the gem has been used internally by the Honeybadger team for about six months.

Show me the code!

Here's a simple example using CloudMailin.

`# First, you define an email receiver class
class EmailReceiver < Incoming::Strategies::CloudMailin
  def receive(mail)
    puts %(Got message from #{mail.to.first} with subject "#{mail.subject}")
  end
end

# Then you feed it a Rack::Request object. And you're done. 
req = Rack::Request.new(env)
result = EmailReceiver.receive(req) # => Got message from whoever@wherever.com with subject "hello world"
`

For more detail, check out the Readme.

But how do you use it in Rails?

Using Incoming! in rails is just as easy. Let's build a sample app.

REQUIREMENT: I hate texting. It's just a peeve of mine. But I need to be able to send love notes to my fiancee while she's in class.

Obviously, the answer is to build an email-to-sms bridge!

First, we set up our email receiver:

`# app/email_receivers/incoming.rb
class EmailToSmsReceiver < Incoming::Strategies::Postmark
  def receive(mail)
    send_sms([mail.subject, mail.body].join(": "))
  end
  private
    def send_sms(message)
      # Insert twilio magic here
    end
end
`

Second, we add a controller to pipe requests into our receiver:

`# app/controllers/emails_controller.rb
class EmailsController < ActionController::Base
  def create
    if EmailToSmsReceiver.receive(request)
      render :json => { :status => 'ok' }
    else
      render :json => { :status => 'rejected' }, :status => 403
    end
  end
end
`

Oh yeah, we need a route:

`# config/routes.rb
Rails.application.routes.draw do
  post '/emails' => 'emails#create'
end
`

That's it.

How does Incoming! compare with other gems?

There are other gems in this space. Most notably Thoughtbot's Griddler.

However there are a few places where Incoming! stands out. It:

  1. Supports multiple mail services

  2. Works with any rack application, not just rails

  3. Hands you a standard Mail::Message

  4. Doesn't make assumptions about your use case

Do you have a meme pic for me?

Yes, yes we do.

incoming

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Starr Horne

    Starr Horne is a Rubyist and Chief JavaScripter at Honeybadger.io. When she's not neck-deep in other people's bugs, she enjoys making furniture with traditional hand-tools, reading history and brewing beer in her garage in Seattle.

    More articles by Starr Horne
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like:

    “Everyone is in love with Honeybadger ... the UI is spot on.”
    Molly Struve, Sr. Site Reliability Engineer, Netflix
    Start free trial