Verse of the Day – Part 3: MailChimp templates


Beside getting OAuth 2 setup for Google Apps, the hardest part of this was to come up with a simple way to create and send MailChimp campaigns with a similar layout and look, but with some custom content. My choices seem to be to between:

1. Create the full HTML5 content from scratch, taking care to include the required Mailchimp tags like *|UNSUB|* or *|HTML:REWARDS|*

2. Use the mailchimp.campaigns and mailchimp.campaigns.template_content from the Mailchimp API to take the template from the campaign and inject content in just the places I want it.

So that I could allow users the chance to continue using the Mailchimp template editor and then just provide search-n-replace snippets, I opted for #2. If I wanted to have radically different content style each time or I had content providers that where more savvy with HTML5 skills and tools, #1 would work, but that is not my world.

First, I created a place-holder campaign there I would layout the style and the bits to be replaced with content. All future campaigns would be replicated from this one then altered using the campaign settings and altering the campaign content itself. Settings are easy with code like:

mailchimp.campaigns.update(campaign_id,'options',{ 'Some Title' })

The content is a bit messier. I get the template as a big data structure with mailchimp.campaigns.template_content(campaign_id), search through it for my search-n-replace “tags” then  use the campaigns.update method to inject it back into the content.

    content = mailchimp.campaigns.template_content(campaign_id)
    content.each do |k, v|
      if v.is_a?(String)
        v.gsub!(/\{(.*?)\}/) { message[Regexp.last_match[1]] }
        mailchimp.campaigns.update(campaign_id, 'content', { 'sections' => { k => v } })
      end
    end

While this works for the simple temple I use, it tends to fall apart for templates with nested sections like Mailchimp allows.

Because this is for a “verse of the day”, there is also code in the script to grab HTML formatted verses from ESV api


Leave a Reply

Your email address will not be published. Required fields are marked *