Author: Assaf Elovic

How to build a dynamic Email template

How to build a dynamic Email template

Automated emails have come a long way in the past couple of years. What used to be a text-only email, today contains various forms, dynamic links, and images, depending each company on their design. Let’s build a dynamic email template in under 10 minutes!

Today, receiving formatted well designed stylish HTML emails has become a standard to most companies, which is why adopting this principle over regular text-only emails has become a must.

Developing HTML templates doesn’t require a lot of coding skills, however knowing how to code the template to appear correctly on all devices and old email clients is the real challenge.

In this blog post, I will go through a step by step guide on how to build a cross-platform-compatible dynamic email template via HTML, CSS, and PHP.

Basic guidelines

As described above, the biggest challenge with developing an HTML email template is making sure it’s cross-platform compatible. There are so many email clients such as Google Mail, Apple Mail, Outlook, AOL, Thunderbird, Yahoo!, Hotmail, Lotus Notes and etc. Some of these clients and others are light years behind the eight-ball in terms of CSS support, which means we must resort to using HTML tables to control the design layout if we really want the email template to display consistently for every user.

In fact, using HTML tables is the best way to achieve a layout that will render consistently across different email clients. Think of the template as being constructed of tables within tables within tables…

Secondly, we must use inline CSS to control elements within your emails, such as background colors and fonts. CSS style declarations should be very basic, without the use of any CSS files.

To emphasize the HTML tables rule above, see the example below, where I’ve modified the border attribute of each table to be visible. Please note that the %s is a placeholder where dynamic text and images will be filled as I’ll see soon describe (Scroll to the end to see the final email template):

As you can see above, the whole layout is built by HTML tables. We’ll be using PHP libraries to parse the %s placeholder and fill it with the dynamic text before an email is sent to the user.

Developing the static template

So let’s start programming!

Before we begin the template itself, you’ll need to begin your HTML file with an XHTML document:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Demystifying Email Design</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
</html>

I recommend defining all tables with border=”1″ as seen above since it’s easier to spot errors and see the skeleton of the layout as you go along. At first, let’s create the basic layout:

<body style="margin: 0; padding: 0;">
 <table border="1" cellpadding="0" cellspacing="0" width="100%">
 <tr>
   <td>
     My first email template!
   </td>
 </tr>
 </table>
</body>

Set the cell padding and cell spacing to zero to avoid any unexpected space in the table. Also set the width to 100% since this table acts as a true body tag for our email, because styling of the body tag isn’t fully supported.

Now we’ll add instead of the text ‘My first email template!’ another table which will present the actual email template display:

<table align="center" border="1" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">
<tr>
 <td>
  This is the email template body
</td>
</tr>
</table>

As you can see, the width is set to 600 pixels. 600 pixels is a safe maximum width for your emails to display correctly on most email clients. In addition, set the border-collapse property to collapse in order to make sure there are no unwanted spaces between the tables and borders.

In the example above, you can see that our email template consists of five sections (rows) which is why we’ll create these rows and then add tables accordingly to each in order to complete the template.

<table align="center" border="1" cellpadding="0" cellspacing="0" width="600">
 <tr>
  <td>
   Row 1
  </td>
 </tr>
...
 <tr>
  <td>
   Row 5
  </td>
 </tr>
</table>

At each row, we’ll create a new table in which the mythology is similar to the above. We’ll also add columns accordingly and the right padding to align all objects to reach the desired template.

To view the final HTML template, visit our project Github page.

A few observations:

  1. Add alt attributes where needed, in order to present text instead of images in case the email client was unable to load them properly.
  2. Add %s placeholders where you’d like the data to appear dynamically depending on the email use case.
  3. If you look carefully, the percentage values appear with an extra ‘%’. This is so the PHP library used to make this dynamic, knows how to parse the text properly.

Note! I’ve removed the URLs for security and privacy issues. Feel free to replace them with your own images and personal links.

And that is it! You’ve successfully developed your own email static template. Now let’s get our hands dirty and make it dynamic!

Building a dynamic template with PHP

On the server side, create the email send method below:

function send_mail_template($to, $from, $subject, $message)
{
  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
  $headers .= "From: ContactNameGoesHere <" . $from . ">\r\n";
  $response = mail($to, $subject, $message, $headers);
}

Now if you look carefully back to the template.html file, you’ll see that I’ve added %s placeholders in certain places. More particularly, in the image banner element, and body text.

All we need to do is import the above template.html file, parse it like regular text, and add the relevant text in place of the ‘%s’ and use the above send_mail_template method.

function build_email_template($email_subject_image, $message)
{
  // Get email template as string
  $email_template_string = file_get_contents('template.html', true);
  // Fill email template with message and relevant banner image
  $email_template = sprintf($email_template_string,'URL_to_Banner_Images/banner_' . $email_subject_image. '.png', $message, $mobile_plugin_string);
  return $email_template;
}

After we’ve got that taken care of, we can use both methods and send our very first dynamic email!

Let’s use an example. Say a new user has just verified his email. We’d like to automate that use case on the server side and send the user a ‘Your email has been successfully verified’ email.

Assume we have the users verified email ‘user@user.com’ and the company’s email is ‘company@company.com’.

We can now send an automated email:

$from = "company@company.com";
$to = "user@user.com";
$body_text = "Your email has been successfully verified...";
$banner_image_subject = "account_verified";
$final_message = build_email_template($banner_image_subject, $body_text);
send_email($to, $from, "You email has been verified", $final_message);

Finally! You can now use this methodology any way needed. After sending this example, while applying the GreenIQ’s company images and text, this is the final email template sent to the user:

Simple right?

Check out the full project on the project’s Github page!

Chatbots are here to stay. Here are 4 reasons why.

Chatbots are here to stay. Here are 4 reasons why.

For the past several years, I’ve been dedicating my life to learn, design, build and write about chatbots. The main reason chatbots fascinate me and conversational interfaces, in general, is because they offer the most natural way for humans to interact with machines. Not only is the interaction natural, but also simple, clean and focused on what you need instantly. Think of Google’s search interface. All you can do is input search queries into a little text box. Everything that comes afterward is magic.Chatbots are still in their very early stage due to a few factors:

1. Lack of expectations between what chatbots can do and what users expect (or in other words, bad user experience). This leads to an instant disappointment and therefore to low usability. It starts with the largest bots such as Siri and all the way down to the basic ones, which confuse users about their actual capabilities. In theory, Siri claims she can do almost anything related to your iPhone device, calendar, and other internal Apple apps.

In reality, over 60% of what you ask Siri is not understood or results in general web search results. Just so you can get an idea of what she can do, here’s a list of Siri commands. Did you know she can do most of it? I didn’t before.

2. The need to educate users on creating new habits. The last bot I’ve built was focused purely on the conversation and therefore did not have any buttons or menus. Retention was low, despite the fact that most of the conversations between the bot and users were held successfully. What I’ve discovered, was that the more buttons and menus I added, the more retention grew. Swelly is a great example for successful bots with minimum conversation and maximum buttons.

This behavior led me to the conclusion that the majority of users are still not used to talking with machines naturally, but rather prefer to click on buttons as we’ve been used to for the past 30 years. Secondly, clicking buttons is faster than typing sentences. However, buttons are not faster than voice, which is why I believe voice will eventually dominate the bot space. The transition from buttons to a natural conversation is growing but is still in its early adoption stage.

3. Artificial intelligence might have improved, but is still in its infancy stages. The reason this last and not first is because I truly believe we can build great chatbots with today’s current AI solutions (such as Api.ai, Wit.ai, etc.) if bots were more focused on how to create user habits and to offer a well-designed user experience that meets user expectations.

You can read more about how to do this in a previous post I’ve written on how to improve your chatbot with 3 simple steps. Apparently, AI will only improve with time as more and more data is collected and trained across a multitude of domains.

For all the reasons above and more, we’re still far from seeing the real potential of chatbots. However, there are strong reasons why chatbots are here to stay and will improve exponentially over time:

The optimal Human to Machine interaction

If you think about it, we’ve learned over the past 30 years to adjust ourselves to the complexity and limitations of machines. This adjustment has made via websites, applications, buttons, icons, etc. But in my opinion, the optimal scenario should be just the opposite — Machines should be able to adjust themselves to us humans, both from a natural language understanding perspective and personalization.

Humans should be able to ask a machine anything naturally, instead of having to learn new interfaces, products, and habits for every service they need.

For example, let’s say you’d like to know the weather. Until recently, you’d have to find and pick a service of your choice out of many alternatives and learn how to use it functionally. Now, since every service tries to be innovative and different from its alternatives, this usually results in various UX/UI which means more learning and effort required by users. The optimal solution would be if you could just ask. Thankfully there are many great solutions today for weather assistants (such as Siri or Poncho), and there are much more to come in other domains.

Domain specific AI

Not very long ago, companies built virtual assistants who tried to go very wide and open but quickly realized how hard it is to understand natural language. Going back to Siri’s example, Apple sought to capture many domains to display Siri as the ultimate personal assistant. This ambition failed very quickly. On the other hand, AI solutions that have succeeded, are the ones that focus narrowly on one specific domain.

Take for example Getdango — an AI solution for predicting emojis. They’re doing a great job predicting emojis based on natural language, and it’s due to their narrow focus. Another example is Meekan, a scheduling assistant for teams on Slack. Meekan is a chatbot dedicated to providing the best solution for scheduling events as easily as possible.

The power of synergy where individual bots focus on specific domains is the right approach for solving bigger AI challenges. You can see companies moving in this direction like FB Messenger’s latest release of handover protocol which enables two or more applications to collaborate. More importantly, Amazon partnered with Microsoft to collaborate on Alexa with the help of Cortana to provide a more powerful virtual assistant. If every bot were to focus on one specific domain, the race to AI in a whole would be solved faster and more efficiently. Happily, that’s where we’re heading.

The power of long term relationships

The way most products are designed today is to maximize immediate short term value for users once they enter an application. While the web and mobile applications focus on short term value, bots can and should focus on long term value. Bot developers should focus on how to build relationships with users over time so that value is gained and grows continuously with every single interaction. With time, bots should know enough about what they need, to maximize and personalize the user experience and minimize input friction.

For example, say you’re looking for a travel planning service. The way you would go by it today, is to look at travel sites, fill the proper forms, and teach the site about your preferences and filters every single time. The way a bot should work is to know which information is relevant to learn about the user, like personal information, preferences, budgets, places the user has already been to and etc. Bots should constantly learn from user’s behavior and offer much more personalized responses.

The optimal way you’d be conversing with such a bot after some time would be as follows:

The bot should know by now how many people are in your family, their ages, where you’ve already been to, where you’re at right now, what you like to do, what you don’t like and much more. In other words, the bot shouldn’t be any different from a real travel agent.

To see how much are users willing to share regarding their personal information within a conversation, I’ve researched with one of my bots. The bot started asking users basic questions like “how old are you” all the way to more personal questions like “what are you most insecure about?”. Guess how many of total users answered all questions truthfully?… Over 85%. More specifically, 89% of women answered all questions truthfully, while “only” 81% of men answered all questions. So if you’re a bot developer, don’t be worried about users not sharing their information. Worry about what questions you should be asking to enhance the users long term value. This kind of information retrieval is something today’s applications cannot achieve, and where chatbots have a huge advantage.

Cross platform usability

In just a few years, mobile apps have transformed to must-haves for smartphone users. But despite the increase in app usage and app choices, the number of apps used per user is staying the same, according to a recent report from Nielsen. Most people are tired of downloading mobile apps and learning about how to use new interfaces.

Also, research says that US citizens own in average 3.6 connected devices. These devices can vary from mobile devices to Smart TVs and products like Amazon Alexa. That’s a lot of connected devices! Now obviously, users would like to interact with your service on any device they’re using. But what do you do? Build an application for iOS, Android, Smart TV, Alexa, Smart watch, iPad, Windows and Mac and more? Sounds like a lot of work. And it’s going to be very hard for you to get users to download your app in the first place since they’re already flooded with other apps.

This ever growing challenge is where the beauty of messaging platforms comes in. At present, approximately 75% of all smartphone users use some of messaging apps such as WhatsApp, WeChat, Facebook Messenger, etc. Over 1.2 billion people worldwide have and use Messenger on their devices, all people that have mobile devices have SMS and obviously, most have an email account. The list goes on. Instead of building applications and spending hundreds of thousands of dollars, just focus on building your bots back end. For the front end, just integrate your bot across multiple messaging platforms that are already on users devices, and you’re set. If your service brings value, users will come. More importantly, turns out it’s what users want.


The future and success of chatbots depend not only on the big 4 tech companies but on developers and entrepreneurs who continue to innovate and push boundaries in AI and conversational interfaces. Most of the current mistakes in this sectors are tomorrows improvements and solutions. Eventually, bots will bring great value that cannot be achieved with today’s applications.

To learn more about chatbots go ahead and read the chatbots beginners guide. If you want to start building one, read this post on how to develop a Facebook Messenger bot.