19
Don't get burned by email
When we noticed Gary Bernhardt's tweet about the major outage of SendGrid services we realized, we could get burned as well and looked at how Superface could help.
This tutorial will help you to send transactional emails with the maximum reliability using Superface OneSDK.
By the end of the tutorial, you’ll have an email integration that:
- automatically fails over and recovers between multiple email providers using circuit breaker pattern
- gives you clear insights into your email usage across all providers
- notifies you when things go wrong and actively suggests improvements
The simplest step you can do to improve your application is to use two providers with failover.
For the purpose of this tutorial, we will use example.com as a sender domain and [email protected] as a sender email address. We’ll configure SendGrid to be the primary and Postmark the secondary email provider. Feel free to use your own domains & providers of choice (although currently only Sendgrid, Postmark, Mailgun & Mailchimp are supported).
You need to do two things, to be able to send emails:
- Verify domain or configure single email address
- Get API Key to access provider APIs
SendGrid
How to verify domain
How to get API Key
Postmark
How to verify domain
How to get API Key
Mailchimp
How to verify domain
How to get API Key
The last step is to Create Superface account. It will give you access to details about send-email capability, and to your project monitoring, which we will use later.
You can use any of your NodeJS projects or simply clone our tutorial repository.
If you use our tutorial repository, get started with installing dependencies and start the application,
# Install dependencies
$ npm install
# Start application
$ npm start
then open http://localhost:3000, you should see
![](https://res.cloudinary.com/practicaldev/image/fetch/s--lb425ZOg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iashx6zm3ntm2yxq5tle.png)
The easiest way to add use cases is to use Superface CLI. Its interactive install will guide you through the setup, and will automatically install OneSDK that does the hard work of integrating for you.
The goal is to have two providers (SendGrid and Postmark) with failover and configure SendEmail
usecase to use circuit-breaker as failover policy.
$ npx @superfaceai/cli install communication/send-email -i
After finishing the interactive install, you should see a new folder called superface
. It is a place where all configuration and metadata for OneSDK lives. Also, package.json
and package-lock.json
will be updated because CLI added @superfaceai/one-sdk
as a new dependency.
Now it is time to send emails. It should be implemented in routes/index.js
file.
First you need to import SuperfaceClient
from @superfaceai/one-sdk
package
const { SuperfaceClient } = require('@superfaceai/one-sdk');
The email will be sent when POST request is received (Look for TODO: Implement Send Hello email comment).
Replace the comment and next line with following code
// Create OneSDK instance
const sdk = new SuperfaceClient();
// Load installed profile
const profile = await sdk.getProfile('communication/send-email');
// Use the profile to SendEmail
const to = req.body.to;
const result = await profile.getUseCase('SendEmail').perform({
to,
from: '[email protected]',
subject: 'Superface Resilient Email Tutorial',
text: `Hello ${to} from Superface Tutorial`,
});
// Get and show data
let data;
try {
data = result.unwrap();
} catch (error) {
console.error('Send Email Failed: ', error);
data = { error: 'Uups..' };
}
Now it is time to run it and try if it works.
Start application with
$ npm start
Open http://localhost:3000, fill in your email address, and hit Send Hello
, you should get the message-id as result and receive the email.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--0y9FZwdW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mom0psz7xyxq2spu87pe.png)
Now let's check how failover works.
You don't want to wait for the next outage, to see if failover works. To emulate the unavailability of SendGrid API, you can point api.sendgrid.com to localhost in /etc/hosts
. If you configured a different primary provider, use its respective API base URL.
For unix systems open /etc/hosts
with sudo
$ sudo nano /etc/hosts
Password:
and enter this line at the end
127.0.0.1 api.sendgrid.com
result should be similar to
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 api.sendgrid.com
Now go back to running the application and send a hello to yourself again
![](https://res.cloudinary.com/practicaldev/image/fetch/s--6LhWSG2w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/98qhddi8jr43i083s8yz.png)
It looks the same as before, but there is a small change. The message-id has a different structure. And it is because the email was sent with Postmark instead of unavailable SendGrid.
You can also try how OneSDK will recover, by removing api.sendgrid.com entry added to /etc/hosts
. It must be at least 30 seconds from failover to secondary, to get tried primary provider again.et
If you configured OneSDK with SDK Token, you should also receive an email notification about failover. It contains information such as when the failover happened and the reason.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--6CN6sCuz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69b309s63zxc7kpyqjqq.png)
If you check the dashboard, you can see what profiles and providers your application is using, the number of performs, and when the last failover happened.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--1AKE2wQJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2m318kpuv7ogwkit1d4c.png)
Emails are important in applications we build, without them users are not able to sign in, or worse customers will not receive bought train tickets. Here is how you can make it super resilient with the least effort.
The best part? You can get this level of resiliency and ease of use for any use case out there! Check out Superface and how it works.
19