How Emails are Managed at Stria via SendGrid
How Emails are Managed at Stria via SendGrid Photo by Flavio Amiel on Unsplash

How Emails are Managed at Stria via SendGrid

Email management is one of the foundations of web applications. Almost all web applications need a system that will send and (sometimes) receive emails. If you're also doing mail marketing, it can turn into a laborious job. In this case, you can write your own email service provider or you can integrate a third party providers such as SendGrid or Mailchimp

SendGrid is the third-party e-mail service provider which is used at Stria to send emails and manage campaigns. In this post, we will discuss how Stria manages emails and campaigns through SendGrid.

Table of Contents

One of the Biggest Problems: Spam-Marked Emails

One of the most critical points in mail marketing is preventing mail services to mark emails as spam. SendGrid enables us to implement "Sender Policy Framework" at this point. With this feature, you can get rid of being marked as spam by mail services.

Another point is avoiding to include a "no-reply" in the sender email. Mail providers can evaluate email as spam based on "no-reply" wording.

SendGrid's dashboard is good enough to analyse mails and behavior of customers. Many details about emails such as status', clicks, spam reports can be clearly view in sub-pages of the Dashboard.

Integrating SendGrid to Business Logic

SendGrid's very handful APIs give you to the ability to manage emails programmatically through your backend. Also, there are SDK's for many programming language, such as Node.js, Go, C#, Python. More info about libraries can be found at https://sendgrid.com/docs/for-developers/sending-email/libraries.

Getting Started

First, an API key is needed to get started. This can be done at https://app.sendgrid.com/settings/api_keys

In this post, we will continue with Node.js.
After obtaining the API key, you can install SendGrid Mail Service and Client SDKs for Node.js with yarn:

yarn add @sendgrid/mail
yarn add @sendgrid/client

or with npm:

npm install --save @sendgrid/mail
npm install --save @sendgrid/client

Add the API key to your .env file:

  echo "SENDGRID_API_KEY=<your_api_key_here>" >> .env

 Now we are ready to go!

Sending an email via the Mail Service SDK:

// Import SDK
const sgMail = require('@sendgrid/mail');
 
// Set API key
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
 
// Create mail data
const msg = {
  to: 'test@example.com',
  from: 'test@example.com',
  subject: 'Sending with SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
 
// Send email
sgMail.send(msg);
 

And within an AdonisJs controller:

 'use strict';
// Import SDK
const sgMail = require('@sendgrid/mail');
// Set API Key Immediately
client.setApiKey(use('Env').get('SENDGRID_API_KEY'));
 
class ExampleController {
  async example() {
 
    // Create mail data
    const msg = {
      to: 'test@example.com',
      from: 'test@example.com',
      subject: 'Sending with SendGrid is Fun',
      text: 'and easy to do anywhere, even with Node.js',
      html: '<strong>and easy to do anywhere, even with Node.js</strong>',
    };
     // Send email
      sgMail.send(msg);
  }
}
 

 More examples and use cases can be found here.

Creating a "recipients" list via Client Service SDK:

// Import SDK
const sgMail = require('@sendgrid/client');
 
// Set API key
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
 
// Prepare request data
const data = {
  "name": "your list name"
};
 
// Send request
request.body = data;
request.method = 'POST';
request.url = '/v3/contactdb/lists';
client.request(request)
.then(([response, body]) => {
  console.log(response.statusCode);
  console.log(response.body);
})

And within an AdonisJs controller: 

'use strict';
// Import SDK
const sgMail = require('@sendgrid/mail');
// Set API Key Immediately
client.setApiKey(use('Env').get('SENDGRID_API_KEY'));
 
class ExampleController {
  async example() {
 
    // Prepare request data
    const data = {
      "name": "your list name"
    };
 
    // Send request
    request.body = data;
    request.method = 'POST';
    request.url = '/v3/contactdb/lists';
    client.request(request)
      .then(([response, body]) => {
        console.log(response.statusCode);
        console.log(response.body);
      })
  }
}

 More examples and use cases can be found here.

As seen in the examples above, even though Sendgrid is a good provider, the SendGrid Node.js SDKs are not good enough and they do nothing more than an "HTTP request" module does. This lead us to implement our mailing module on the top of SendGrid SDKs.

In this case, consider Singleton Architecture while registering your custom mail provider. You can register the class at hooks.js for AdonisJs .

const {hooks} = require('@adonisjs/ignitor');
const {ioc} = require('@adonisjs/fold');
 
hooks.after.providersBooted(() => {
  const CustomMail = use('App/Path/To/CustomMail');
  ioc.singleton('Custom/Mail', () => {
    return new CustomMail();
  });
}

Read more about Adonis.js concept here

Documents and Academy

Documents are the most important part of APIs because APIs that don't have good documents are like a keyless treasure! Thankfully, SendGrid has well documented APIs. And also there is a playground which you can send sandboxed requests to the endpoints. Try it out here: https://sendgrid.com/docs/API_Reference/api_v3.html

Sendgrid API Docs & Playground
Sendgrid API Docs & Playground

SendGrid documents for many programming languages also can be found at SendGrid GitHub Page.

SendGrid also offers training courses under the name of SendGrid Academy that can increase efficiency of your mail management.  and enable us to establish high quality communication with our customers. 

In Conclusion

SendGrid has dashboard that allows us to do mail marketing easily. It is also has an API reference page and SDKs for implementation to our system. However, improvements for SDKs -at least for Node.js- are needed. Also, this may make you to write your own mail provider to top of official SDKs. In such case, you should consider singleton architecture especially if you use a framework which support it like Adonis.js . 
Finally, in point of learning mail management, SendGrid Academy helps us. As a result of all this, email management becomes easier. 

 

This article was updated on January 13, 2019