Generating emails in multiple languages

We have a client that needs to send the same email to a list of different regions around the world, which of course means translating that email. Not only that, but the links in each email need to point to a different site depending on the region, and the local store address for that region needs to be listed, sometimes at the top of the email, sometimes at the bottom, sometimes both... and then there's other things like the Facebook link/logo isn't relevant for China, so that needs to be replaced with Weibo. It's a big job.

I was brought onto the project after a colleague left and promptly discovered that there was no automated system for producing these emails - they were built by hand (which is fine) and then copied and manually adjusted for each of the twelve regions (less fine). My first real taste of how painful and time consuming a process this could be came during my second round of work for this client, when they produced two designs instead of one, that they wanted to send to half of each region. Now I had twenty four emails that needed editing and tweaking, a process that was long and painful, not only because the links for the various regions kept changing but also because whenever the client decided they wanted e.g. the font a bit smaller, I had to do the same change in 24 separate files.

After that took up something like a week of my time, spent mostly going line by line and copying and pasting text and links from an excel spreadsheet, I decided enough was enough. By strange coincidence I was working on some PHP code at the time for this very website, to replace variables within templates, so I took a few hours and built this.

Doesn't look much, does it? Let's go through some requirements. Basically, I needed a system that could process a template and replace every instance of a named variable with one of the following:

  • A string of text, such as a product name
  • A URL (basically the same as text)
  • An unknown set of arbitrary markup, such as the afore-mentioned facebook link

I spent a few hours on some coding, and the email generator was the result. You give it a template file and a variables file and it'll spit out the generated HTML for you. What that means is that if the client suddenly decides the font in the title should be greener, you can just modify it once in the template then regenerate all the output. Both the template and the variables can be anything you like. Here's a simple example of a template.

<table width="100%" cellpadding="0" cellspacing="0">
    <td>{{ title }}</td>
    <td>{{ content }}</td>

The accompanying variables file could look like this.

    <email filename="en-uk">
        <variable name="title" value="Title of en-uk"/>
        <variable name="content" value="This is the content of my first email"/>
    <email filename="en-us">
        <variable name="title" value="Title of en-us"/>
        <variable name="content" value="This is the content of my second email"/>

The generator would then output two files, en-uk.html and en-us.html, each containing the relevant variables. It's not online anywhere because it's for internal use only and adding in the required security for public hosting would have taken too much time (it not only creates the required HTML but writes it to a file as well).

Complex variables including markup require the use of <![CDATA[, but otherwise the process is the same. The great thing about it is that a variable can be anything from a few characters to an entire chunk of HTML, making it very flexible to use. In theory it could even be used for another, similar purpose not involving emails - I've just not come across one yet.

I've used it to generate a few different email campaigns now and it's proved an enormous time saver. I've also spent some more time on it since then to improve it, correcting a few things and adding a few extra features, such as notifications if a variable in the template hasn't been filled. A few other people in the office have even started using it as well, so I'm counting it a success.


This article is tagged with