View on Github View on Rubygems.org

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!



Published on 19 February 2013



Starr Horne is the driving force behind Honeybadger's single-page UI. He works around the clock to find better ways for you to slice through mountains of debug data and get to the heart of your issues in record time.