2015-08-26

Let’s face it. Whether your application user base is 5 or 5 million, you will eventually have a need to email one, some or all your users. This stands true for almost all types of apps – web, desktop or mobile. Carefully-crafted, well-timed emails is one of the keys to a happy user base.

While the need is obvious, setting up email automation is, however, not for the faint-hearted. Your options are to host an SMTP server yourself, if you are really brave, or pay a lot of money for bulk email services.

Wouldn’t it be nice if email automation was developer-friendly and you could cut out all the data exports between systems? What if emails could be completely customized with templates and placeholders, and you had programmatic access to push out emails with the exact dynamic payload you want? Wouldn’t it be cool if the very place where you store your application and user data also offered email automation out-of-the-box? Are you wondering why I ask so many rhetorical questions?

Thankfully I have the perfect answer. Telerik Backend Services offers a “one stop shop” for a complete cloud-based MBaaS (Mobile Backend-as-a-Service) solution, complete with rich email automation features. Let’s jump in!

Setting Up

Telerik Backend Services offers cloud storage/computing, user management, social authentication and cross-platform mobile support – all the while being secure and scalable. But you may not realize that it also offers robust set of email automation tools as well.

While there are built-in email flows around user management (welcome, verify account, reset password, and password change emails), you may often be needing simple, stand-alone, template-based email automation for your application needs. Here’s how to get started.

First, head over to https://platform.telerik.com and sign in with your preferred identity provider. If this your first time, a quick and free sign up is all you need to get going with the Telerik Platform.

Once you are logged in, create a new App and then add a new Backend Services project to your app, as shown below. An App is a placeholder for the different components that make up your Mobile app solution – you are only adding the cloud backend piece here.



Once inside your Backend Services project, click on Configure, then Services and add Email Notifications, like below.



Templates for Everything

The addition of Email Notifications services to your Backend Services project starts giving you options towards templated email automation. Not surprisingly, you begin by setting up Email Templates. Simply click on the Add an Email Template button after selecting Templates from the navigation menu.



The template includes everything you’d expect to see: sender/receiver information and the all-important email message body, as shown in the Template definition sections below. The Template Name is mandatory so that you have a way to refer to a template down the line.

Handy Placeholders

As you are defining your email template, you can fill in the pieces with static content like a Subject or From field. But very often, you will be choosing to insert dynamic data as content. Enter Placeholders.

As the name suggests, Placeholders in email templates are simply ways for you to inject dynamic content as you automate email flows later on. Placeholders are defined within the double braces ‘{{ …}}‘, as shown below.

One interesting thing to note is in the email message body – you will most likely use one or more placeholders in the body. The WYSIWYG rich text editor used to compose the email message body, will actually honor the text formatting you define around the Placeholder, as shown below. You’ll see the impact of this when sending emails down the line.

The Placeholders you add to your email templates are entirely yours to name – after all, it will be you filling in the corresponding matching dynamic pieces later. However, you are free to use a couple of System Placeholders which the email service already knows about, like some Defaults shown below. These System Placeholders are built-in variables that you could manage as Settings.

Once you have set up your email Template to your satisfaction, Backend Services will show you the template that you just saved. You can edit the Template as many times you want until is exactly what you need.

The first part of your email automation is now ready – you have all the placeholders lined up. Now, you just need to fire up it somehow.

Cloud Automation

You can trigger the email automation around the template you created in several different ways. The easiest way is to use something called Cloud Functions whereby your JavaScript code runs seamlessly in the cloud, without you having to host anything on your own. To get started though, you have to configure another service to be added to your Backend Services project – time to add Business Logic service, as shown below.

Your Code in the Cloud

Once you have added Business Logic services, click on Explorer and create a new Cloud Function.

When the code editor window pops up, you’ll type in a JavaScript function.

Here is the exact code to enter:

Looks familiar? Yes, this is Node.js on your Backend Services cloud server. Everlive is an internal codename for Backend Services, and you are essentially defining a JavaScript function to handle a request/response.

The request is to fire up the email service and the response is the result of this action. As you can guess, the function makes sure to only honor HTTP Post requests and grabs information off of the request – like email TemplateName, Recipients (array of email addresses) and Context.

If everything looks good, the sendEmailFromTemplate() method is invoked to actually try sending emails.

Fire it Up

Now that you have a cloud function that can be invoked to test out the email automation, you’ll find the big Execute button rather enticing. Pray to the demo gods, and click on the button.

What comes up is the pre-execution confirmation pop-up – you’ll see the unique URL that is the endpoint for your custom Cloud Function. Remember to change the Request type dropdown to POST, since that is what the function is expecting. The next thing expected of you is the actual email Payload – this is in the form of a simple JSON object.

The email payload is the all-important dynamic content that makes up your custom email. This is where you get to name the email template you want to use (i.e. the one you defined earlier), along with recipient email addresses and a context object. The context object contains the named key-value parameters, which are mappings to provide values to the placeholders that you defined in your email template. See how all the pieces fit together now?

Here’s a sample payload, in case your app was to send out sample shipping notification on items sold:

Here’s what it should look like in the execution screen:

Now that you have provided the information needed for the cloud function to execute and all the dynamic content for the email template placeholders, it’s time to hit the final Execute button, like below.

Boom! Just like that, the cloud function takes your Payload and executes the email functionality, picking up the named template on its way. The response is visible immediately in the Response window and an HTTP Status Code of 200 means everything ran perfectly!

If you had used a legitimate recipient email address, sure enough – you get an email, as shown with the defined payload.

The From email address can also be customized through the use of a placeholder. The email message body is also italicized – meaning the message composing editor honored the formatting around the message body Placeholder.

REST Invocation

While the UI based execution of the Cloud Function may have been cool for one time, it was a rather manual process. You would likely immediately begin looking to automate the process so that you could trigger the email functionality from anywhere, and as many times as needed.

Don’t worry. Every cloud function essentially becomes a RESTful endpoint – a unique URL through which to invoke your function. Simply make the corresponding HTTP POST request at the corresponding Cloud Function URL – and, voila, the email functionality will be faithfully triggered using all of your dynamic content.

Also, as you invoke cloud functions more often, you may want some traceability for recording or troubleshooting. Backend Services logs every Business Logic request/response, including every time your cloud function is invoked to send out emails. Simply click on Logs as below – and you’ll find that all request/response details are recorded every time you invoke your cloud code.

Conclusion

Email automation is often a necessary evil, that is either too complicated or too expensive. With Backend Services, you are already storing most of your application and user data in the cloud. The built-in email automation comes as an added bonus – and everything is within the same cloud project.

You get to utilize customizable email templates with placeholders for dynamic content and standard HTTP communication to invoke cloud functions through RESTful endpoints. The heavy lifting is taken care of for you.

Appropriate communication at just the right times equals happy users, and happy users equals happier developers!

Header image courtesy of Sean MacEntee

The post Email Automation Simplified appeared first on Telerik Developer Network.

Show more