Ruby On Rails Transactional Email Using MailChimp And SendGrid


mailchimp sendgridSince launch mightyvites.com has sent email through Gmail using very basic HTML/ERB templates. There have been no graphics, little styling, and zero email client testing. In other words our emails have sucked and looked very unprofessional. Why? Because those are the last things you worry about when you’re trying to push out the door a product that you’re not sure anyone will buy. Once your site has users, however, these are the bells and whistles you need to put time into.

Recently the time came for me to spruce up the emails mightyvites.com sends. I wanted slick-looking HTML that looks good in all email clients, makes it to users’ inboxes, is multipart, and can be tracked. In addition the email templates should be easy for non-geeks to edit. That way they can be changed often and not always by me.

I’m a fan of MailChimp. We use their (robust) API to programmatically manage our newsletter subscriber list. They make it very easy to compose, send, and track well-designed HTML email. They do not, however, make it easy to send transactional email. That’s not their niche so they don’t cater to it 1. There is a work around, but it leaves much to be desired. MailChimp transactional emails must include unsubscribe links and other CAN-SPAM compliance content which is just out of place on notices like forgotten password emails. The MailChimp email composer, however, is pretty sweet. It’s easy to use and generates cross-client compatible HTML email complete with text versions for multi-part. The composer was perfect for my needs, but sending through MailChimp was no good because doing so automatically adds the unwanted mailing list links. Then it hit me: if I could compose and save my emails in MailChimp and later export them I could include the export in my Rails app and send through the email provider of my choice. Possible? With hominid, the MailChimp API Ruby gem, yes.

Hominid wraps the well documented MailChimp API for use in Ruby applications. It adds the syntactic sugar most Rubyists would expect and pretty much does a 1 to 1 mapping of the methods MailChimp defines. Part of the API deals with what MailChimp calls templates. Templates are reusable emails that you create at mailchimp.com and save to your account. You can access these HTML templates through the API and run them through a method to generate a text version. With both a HTML and text version “in hand” you can write the output to disk and have what you need for ActionMailer to generate fancy, multi-part emails that were edited online with MailChimp’s slick editor. I keep all this action in a Rake task which I run every time I update a template at MailChimp.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  desc "Task to pull customer email templates from MailChimp and cache them in the app"
  task :import_email_templates => :environment do
    # Maintain a map of ActionMailer methods to MailChimp
    # template names in a YAML file. YAML looks like:
    # <mailer class name>:
    #   <mailer method name>: <name of MailChimp template used by method>
    # Example:
    # user_mailer:
    #   signed_up: "Welcome To Mightyvites"
    #   ...
    map=YAML.load_file(MAILER_TEMPLATE_YAML)
    api=Hominid::API.new(MAILCHIMP_API_KEY)
 
    # loop through the list of known MailChimp template names
    api.templates['user'].each do |template|
      html_cache=nil
 
      # find the mailer class and method this template belongs to
      map.each do |mailer, emails|
        template_name=emails.invert[template['name']]
 
        if template_name
          html_cache=File.join(Rails.application.config.view_path, mailer.to_s, "#{template_name}.html.erb")
          break
        end
      end
 
      next unless File.exist?(html_cache)
 
      # pull the latest HTML templates from MailChimp
      html=api.template_info(template['id'].to_i)['source']
 
      # create a text version of the HTML for multi-part mail
      text=api.generate_text('html', html)
 
      # inline all CSS to ensure greatest email client compatibility
      html=api.inline_css(html)
 
      # overwrite mailer views
      File.open(html_cache, 'w+') {|f| f << html }
      text_cache=html_cache.gsub('.html.erb', '.text.erb')
      File.open(text_cache, 'w+') {|f| f << text }
    end
  end

With that code I can easily update my application’s mail templates and manage them at MailChimp. My templates don’t change often so I keep the update manual, but you could set up a cron job to pull in changes nightly, or create a Capistrano task to pull in a fresh copy with each application deployment.

So what about tracking? That was the last wish on my list and was easily gotten with a nifty SMTP service called SendGrid. Goodbye Gmail! SendGrid works extra hard to give my application a high email delivery rate and it provides a nice web interface for viewing all email sends, clicks, bounces, etc. SendGrid’s tracking isn’t as slick as MailChimp’s, but it’s a heck of a lot better than nothing. Overall I’ve been very pleased with SendGrid’s service, and happy as a clam with my MailChimp’d ActionMailer templates.

  1. In response to Amazon’s recent foray into transactional email MailChimp created a service on top of SES.

Incoming search terms:

  • sendgrid templates
  • rails transactional email
  • sendgrid email templates
  • mailchimp rails
  • sendgrid mailchimp rails
  • rails mailchimp tutorial
  • mailchimp transactional emails
  • sendgrid mailchimp
  • rails mail client
  • tracking emails sent through rails application using mailchimp

Leave a Reply