We don't update our blog frequently enough - I'm pretty sure we aren't alone in this. However, I do tend to send many emails per week to the rest of the developers here at Isotope11 either outlining various things I'm working on, or linking to neat ruby libraries I've found, or interesting web designs, or funny pictures of dogs.

Also, we're currently working on a rails engine that will provide drop-in support for Github webhooks. We'll be using this integration to make it dead simple to tie work units in our open source time tracking system, xrono, to the commits related to the work. With that in mind, I set out to make it equally as simple for us to post to the blog without having to take any extra effort.

CloudMailin

There's a service called CloudMailin that I came across once I'd decided to do this. They seem to be the best web service for quickly building support for incoming email into your application. Their free plan supports 200 emails per month, which should be more than sufficient for our needs here (though I am sure we'll use them for client projects that will require a paid version in the future, since they're so awesome.) I have no affiliation with these guys, I just love what they're doing since it's precisely what I wanted, at the right price.

Getting started

Go to their site and sign up for a free account. Then check out their docs. Their site is a rails-based site, so the ruby integration comes up front and center. They just give you an email address, and for each incoming email they hit you up with a webhook.

From there, it's painless to parse the subject line of the incoming email for [BLOG]. Then I take the rest of the subject as the post's title, and run the body of the email through a markdown parser. Just like that, it becomes really simple to post to our blog, and I'm unlikely to let it languish any longer :)

Aaaaand the code

Here's the entirety of the code powering this feature, for reference:

class IncomingMailsController < ApplicationController
  require 'mail'
  skip_before_filter :verify_authenticity_token

  def create
    @message = Mail.new(params[:message])

    if is_blog_post? && is_by_authorized_user?
      generate_blog_post
    end

    render :text => 'success', :status => 200 # a status of 404 would reject the mail
  end

  protected
  def generate_blog_post
    @post = Post.new
    @post.user = blog_post_user
    @post.title = blog_post_title
    @post.body = blog_post_body
    @post.save
  end

  def is_blog_post?
    @message.subject =~ blog_post_identification_regex
  end

  def is_by_authorized_user?
    blog_post_user
  end

  def blog_post_user
    @user ||= User.find_by_email(@message.from[0])
  end

  def blog_post_title
    @message.subject.gsub(blog_post_identification_regex, '').strip
  end

  def blog_post_body
    RDiscount.new(@message.parts.first.body.to_s).to_html
  end

  def blog_post_identification_regex
    /\[BLOG\]/
  end
end

Josh Adams is a developer and architect with over eleven years of professional experience building production-quality software and managing projects. Josh is isotope|eleven's lead architect, and is responsible for overseeing architectural decisions and translating customer requirements into working software. Josh graduated from the University of Alabama at Birmingham (UAB) with Bachelor of Science degrees in both Mathematics and Philosophy. He also occasionally provides Technical Review for Apress Publishing, specifically regarding Arduino microprocessors. When he's not working, Josh enjoys spending time with his family. <a href="http://www.erlang-factory.com/conference/show/conference-6/home/"><img src="http://www.erlang-factory.com/static/upload/media/1389191028314604speaker120x125gif" alt="speaker badge" /></a>