Mandrill on Rails, Monkey Conductor

Email sucks, but yours doesn’t have to. There are many things to consider when implementing email in your application, modern HTML elements and CSS don’t work, no JavaScript, email reputation and deliverability. It may seem counterintuitive but taking email rendering and styling out of your Rails application may be the best thing you do this year. We recently stopped styling and rendering the transactional emails from within Rails and instead use the mandrill_mailer gem to pass variables to templates stored on Mandrill. This gives us the added benefit that our Mandrill templates are easily ported back and forth with Mailchimp, which we use for mailing list management and delivery.

This has had some very real gains in our productivity. Our designer no longer needs to know how to trigger the email, instead only has to worry about building the template and then pasting it into Mandrill’s admin interface. Also, it gives realtime previews across devices and offers a myriad of templates to start with that support responsive designs with tables and allows us to easily conform to legalities like Spam Acts.

Great! We all agree to let the people with 10+ years experience in email handle that for us. Now, how do we leverage that in our application?

Setup

First, we’ll need to install the mandrill_mailer gem.

Gemfile

gem "madrill_mailer"  

Terminal

$ bundle install

Configuration

Looks like we’ll need to go setup a Mandrill account as well. To do that, go here. Then retrieve your API_KEY from here.

Once you have those credentials we can start to implement our new fancy emails. We’ll start by adding the initializer for mandrill_mailer and it will look like this.

config/initializers/mandrill_mailer.rb

MandrillMailer.configure do |config|  
  config.api_key = ENV["MANDRILL_API_KEY"]
end  

In order for this to work, you’ll need to load the API_KEY you received from Mandrill into your environment, both in production and development. Read more about this, here.

You’ll also need to set a default configuration for your application.

config/application.rb

module Codelation  
  class Application < Rails::Application
    # lines omitted
    config.mandrill_mailer.default_url_options = { host: ENV["HOSTNAME"] }
  end
end  

Implementation

We have all the ground work done now to be able to actually use mandrill_mailer.

Now we get to the point where you will need to implement or reimplement all of your needed mailers in this new format. It’s relatively simple, generally you’ll change only a handful of things.

  1. Remove all current mailer view files, app/views/contact_message_mailer/*.html.erb.
  2. Figure out which variables are needed for each mailer.
  3. Create the template(s) on Mandrill with those variables. See here for more information.
  4. Write/rewrite all mailers in the following format.
class ContactMessageMailer < MandrillMailer::TemplateMailer  
  default from: "[email protected]"

  # When someone creates a new contact message we will send an email to ourselves.
  def contact_message(contact_email, message)
    mandrill_mail(
      template: "contact-message",
      to:       ENV["CONTACT_EMAIL_ADDRESS"],
      subject:  "[Codelation] New message from #{contact_email}",
      vars: {
        "EMAIL"   => contact_email,
        "MESSAGE" => message
      }
    )
  end
end  

Nothing much changes besides the change from mail() to mandrill_mail() and then the ability to pass in variables. Now, all that’s left is to send an email!

ContactMessageMailer.contact_message("[email protected]", "Hey guys, how are you doing?").deliver  

Thanks for reading!