Using SparkPost Templates with C#

Mike Hillyer
Oct. 2, 2017 by Mike Hillyer
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”:

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:

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

 

 

Share your Thoughts

Your email address will not be published.

Related Content

Introducing Our SendGrid Template Migration Tool

Automatically migrate any SendGrid template over to SparkPost. This open source tool supports both UI- and API-driven email template migration workflows.

read more

Leveraging Substitution Data and Templates to Personalize Emails

Jeff Goldstein explains how to leverage substitution data and templates to separate the work of obtaining data for personalized emails and the creative for those emails.

read more

Our Top 5 Email Template Hacks

From creating standards that can apply to many templates to design hints on validating your data, here are our top 5 email template hacks.

read more

Start sending email in minutes!

The world’s most powerful email delivery solution is now yours in a developer-friendly, quick to set up cloud service. Open a SparkPost account today and get started for free.

Get Started

Send this to a friend