Sending Email using SendGrid API

Posted on Sep 13, 2017

Sending Email using SendGrid API

While choosing the correct email service for any project, the foremost thing that comes to our mind is whether the email send using email service will be successfully sent or not, whether it will get delivered? If yes then will it get timely delivered? A permanent solution to all above question is SendGrid Email Service, providing real-time analytics and reliable transactional email delivery.

What is the SendGrid Email Service?

SendGrid is a cloud-based email service providing reliable transactional email delivery, scalability, and real-time analytics along with flexible APIs that make custom integration easy. Common SendGrid use cases include:

  • Automatically sending receipts or purchase confirmations to customers.
  • Administering distribution lists for sending customers monthly fliers and promotions.
  • Collecting real-time metrics for things like blocked email and customer engagement.
  • Forwarding customer inquiries.
  • Processing incoming emails.

For more information, visit https://sendgrid.com or SendGrid’s GitHub repo.

To send an email using SendGrid API, follow the steps:

Step 1: Create a SendGrid Account

Before we move to SendGrid implementation, we need API key to access SendGrid API. For accessing SendGrid API key, we need to create an account on SendGrid.

To create an account on SendGrid do the following:-

1) Go to official website of SendGrid, https://sendgrid.com
2) Now click on sign up on top right corner

3) Next, choose your plan according to your need,

4) Now enter your Username email and password on the signup page.

5) Your sign up procedure is complete.

Step 2: Find your API key

Generate an API key from your SendGrid account:

Step 3: Send Email using SendGrid (JavaScript)

To send an email using SendGrid, you need to call SendGrid API using generated API key.

1) Install ‘sendgrid’ using npm library,

npm install --save sendgrid

2) Now setup API key to use it in code,

settings.sendgrid = {
 key:"Paste your API Key here",
 }

3) Now require sendgrid in your file,

const sg     = require('sendgrid')(settings.sendgrid.key);

Here, ‘sendgrid’ is a npm package and ‘settings.sendgrid.key’ is the API key.

4) Create an object containing email details using mail function of ‘sendgrid’.

const helper        = require('sendgrid').mail;
 SendEmail () {
        this.fromEmail  = new helper.Email("Sender Email");
        this.toEmail    = new helper.Email("Recipient Email");
        this.subject    = "Subject";
        this.content    = new helper.Content("text/html", "Email Message");
 
        const mail = new helper.Mail(this.fromEmail, this.subject, this.toEmail, this.content);


        return mail;
 }

In above code, SendEmail is a function to get mail object containing details of sender, recipient, subject of the email and email content. We will use this email object to create a request to SendGrid API.

5) Now using ‘sg’ created in the third Step, we will create a request to SendGrid,

const request = sg.emptyRequest({
            method : 'POST',
            path   : '/v3/mail/send',
            body   : mail.toJSON(),
        });
 
 sg.API(request, (error, response) => {
            // response of request send to SendGrid API
 });

As you can see, we get a response of SendGrid request. This is how we use SendGrid API in javascript to send an email.

Hope you found this blog helpful, If any query please let me know.