Sending email using sendgrid's API and node.js







Overview

In this tutorial, we will learn how we can send email using sendgrid's API and node.js .

Prerequisites

  1. Node.js installed : You can download it here .
  2. Sendgrid account : Follow the steps given below to create an account on sendgrid and send email using it.
  3. Sendgrid NPM package

Sendgrid

As per wikipedia :

SendGrid provides a cloud-based email delivery service that assists businesses with email delivery. The service manages various types of email including shipping notifications, friend requests, sign-up confirmations, and email newsletters. It also handles internet service provider (ISP) monitoring, domain keys, sender policy framework (SPF), and feedback loops. Additionally, the company provides link tracking, open rate reporting.It also allows companies to track email opens, unsubscribes, bounces, and spam reports.
Click here to create an account on sendgrid and after that follow the instructions given below :

  • We are selecting try for free option for this tutorial as shown below :

    SendGrid Installation


  • Now input the required details which includes :
    1. Username : This needs to be unique.
    2. Password : Enter a secure alphanumeric password here.
    3. Confirm password : Enter the password again to confirm it.
    4. Email : Enter a valid email address here which you want to link with your sendgrid account.


    SendGrid Installation


  • Your account is created Now input details about you in order to proceed. We've marked the details we entered while creating this tutorial in the image given below :

    SendGrid Installation


  • Congratulations! you have successfully created account on sendgrid. Now move to the guides section to create your first application. You can click on the guides provided in the help link given on the bottom right corner of dashboard as shown below or you can simply click here .

    SendGrid Installation


  • Now click on the start link provided in front of " Integrate using our Web API or SMTP relay " as shown below :

    SendGrid Installation


  • Now we need to choose the setup method. We are using Web API method for our tutorial which , as per sendgrid , is fastest and most flexible way to send email using languages like Node.js, Ruby, C#, and more.

    SendGrid Installation


  • Choose node.js as the language you want to use as shown below : SendGrid Installation


  • Now we need an API key. We can create an API by adding some random content as the name of the API key and clicking on the create API key button as shown below: SendGrid Installation


  • Now you need to update development environment with SENDGRID_API_KEY by running the following commands in your shell :
    										
    echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
    echo "sendgrid.env" >> .gitignore
    source ./sendgrid.env
    										
    									

    The screenshot of the following is given here :

    SendGrid Installation


  • Install the sendgrid NPM package with the following command :
    										
    npm install --save @sendgrid/mail
    										
    									

  • Now copy the minimum code required to send an email as shown below :
    SendGrid Installation

    You can copy it from here in a email.js file :
    										
    //Name of the file : email.js
    // using SendGrid's v3 Node.js Library
    // https://github.com/sendgrid/sendgrid-nodejs
    const sgMail = require('@sendgrid/mail');
    sgMail.setApiKey(process.env.SENDGRID_API_KEY);
    const msg = {
      to: 'test@example.com',					//receiver's email
      from: 'test@example.com',			//sender's email
      subject: 'I think its working',				//Subject
      text: 'and finally  i can send email from sendgrid',		//content
      html: 'and easy to do anywhere, even with Node.js',			//HTML content
    };
    sgMail.send(msg);
    										
    									


    you can run it using the following command :
    										
    >node email.js
    										
    									


    And if it runs without any error you can click on the I've integrated the code above check box and click on verification button and sendgrid will check whether your mail came through or not.


Summary

We learned how we can send a mail using sendgrid.