Transform your New User Welcome emails with C# and SparkPost Click to Tweet
If you take a look at the workhorse transactional email in your inbox—things like receipts, shipping notices, password resets, and the like — I bet you’ll notice there’s an awful lot of ugly emails. A company will spend time and money on a brand, on designing a compelling website, and on advertising, but when it’s time for their automated systems to send an email to the user, you get something like this:

All that work, and it’s thrown away on a plain-text email with no design at all. Why? Because design and code don’t mix well together. Most automated messages are built line by line in plain text and then sent using a .Send() function. Marketers don’t like it because they can’t update the look and feel of the messages to match the rest of their work and developers don’t like it because it clutters their code. They also end up getting constantly asked to change a line here, a word there, etc.

There is a better way. SparkPost’s built-in templating capabilities allow for a separation of design and code. Instead of trying to embed the content of your messages into your code, you can simply and easily upload your templates to SparkPost and call our APIs with a template reference rather than passing the formatted message content. Not only is this easier than techniques such as parsing Razer pages to strings, but it also moves the template out of the application completely, leaving the developer to focus on code.

Let’s get started!

Sending a Template Email Through SparkPost with C#

If you’re following along at home, we started with a quick example of how to send a C# email using the SparkPost API (and if you’re not, read it now to see how easy it is to send a C# email message using SparkPost, I’ll wait). Our next goal is to remove the message design elements from our code and replace them with a stored template.

First, we need to create and store a template. Start by reading our guide to managing templates and set up a template. For this example, I’ll use the following HTML template in a template named “My First Email”:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<p>Hi {{ Name }};</p>
<p>Here are our upcoming events:</p>
<table border="1">
 {{ each UpcomingEvents }}
   <td>{{ loop_vars.Name }}</td>
   <td>${{ loop_vars.Date }}</td>

The SparkPost templating language is based on HandleBars and is straightforward to use. In this case, substitutions occur inside the {{ }} blocks, and SparkPost templating supports logic, basic operations, and even looping through arrays. For more information, see the Substitutions Reference in our API Documentation.

Once our template is stored and published, we can use it in a call to the transmissions API:

var transmission = new Transmission();
transmission.Content.From.Email = "";
transmission.Content.TemplateId = "my-first-email";
var firstRecipient = new Recipient
	Address = new Address { Email = "" }
var secondRecipient = new Recipient
	Address = new Address { Email = "" }
// template data can be passed on a per-recipient basis
firstRecipient.SubstitutionData["Name"] = "Mike";
secondRecipient.SubstitutionData["Name"] = "Bob            	
// template data can also be passed on a per-transmission basis
transmission.SubstitutionData["UpcomingEvents"] = new []
new { Name = "Thanksgiving", DueDate = new Date(2017, 11, 23) },
	new { Name = "Christmas", DueDate = new Date(2017, 12, 25) }

var client = new Client("<YOUR API KEY>");
var response = await client.Transmission.Send(transmission);

And that’s it. We create a transmission, create a recipient, add template substitution data to the recipient, add the recipient to the transmission, and send the transmission to the SparkPost API. If you wanted to send to more than one recipient, you would simply add more recipients to the transmission before sending.

Your code is focused on triggering the message and filling in the blanks, the design is completely separated out and not a concern. Is it less ugly than the example that I started with? Well, no (I’m no designer), but at least now your design team can log into the SparkPost web UI and make it prettier without asking you to change code to improve the design!

Next up on your journey

You’ve taken the next step to being a C# email master by separating design from sending logic. You have a working template, you’ve sent it in a message, and your design team can adjust the look and feel of the message without your involvement!

Once again, your journey is not yet complete. Is it enough to send a message, without having any idea whether people are receiving it? No! Stay tuned, and I will show you how to query the metrics API to determine how your sending has gone.

Till then, sign up for SparkPost, learn how to manage templates via API, and check out our great blogs on templates! If you have any questions come to the #csharp channel in community slack.

— Mike