Send Emails Through Node js And Sendgrid Service

Shanuka Prabodha Deshapriya
5 min readMay 9, 2021

Hello guys,Today I will guide you to send emails through Node js by depolying sendgrid third party service that was the recently I learnt during my campus project in Application Framework Module.

What is SendGrid ?

SendGrid is the email delivery service.It allows to send email using Node js.

Let’s Get Started

First,we need to create and configure an account for SendGrid.You can follow the link given below :

Please create an account for the SendGrid ,I hope that will easy for you ,so I’m not going to do that. Now I’m going to configure my SendGrid account. You can follow the given steps in below :

After creating the account you will see the dashboard.

SendGrid Dashboard

Now we have to create sender identity.so that click the button “Create a single Sender”.Then you can see the form.

Create sender form

In this form you hava to fill all fields,

From email address — sender’s email address,

Reply to — Reciever’s email address

I hope you can fill other feilds as you wish without any doubts.After submitting the form you will see the summery of that and also verification email is sent to the Reciever’s email address that is mention in the above(you typed in form).

you will see in the summery , verified mark display as close icon . so you have to go to that receiver gmail and verify the link.After verifing the email ,please refresh the webpage and you will see verified mark has changed as right icon.

Summery of sender identity before verifiing
Summery of sender identity after verifiing

Now we have to create an API key for our development.so that you can go Settings that is in left side bar and then click API Keys.

Create API Page

after clicking the Create API button you have to give the following :

Create API

Then click create & View button.Then you will see the API key that is important key for our coding.you have to save this API in notepad or something else because this API not show you again dute to the security resons.

Do not put API key in public github repositories. It will suspend your account and you have to request account again from the sendrid .

API KEY

Ok now we done the SendGrid configuration.Let’s move to the coding part.I use vs code as IDE.

First I create Folder as SEND_EMAIL and create sub folders as email_backend.

Go to email_backend folder using cmd by giving command as cd email_backend and init a npm project.

cd email_backendnpm init

After that install the following npm packeges.

npm install express cors npm install nodemailer-sendgrid-transportnpm install nodemailer

Now I create server.js file inside the email_backend folder and add the following code.

const express = require("express");const bodyParser = require("body-parser");const cors = require("cors");const app = express();const PORT = process.env.PORT || 8070;app.use(cors());app.use(bodyParser.json());app.listen(PORT,()=>{console.log(`Server is up and running on port ${PORT}`)});

use npm start dev to run the project.

you can refer my vs code given in below :

server.js

now I create folder name as “router ” inside the email_backend folder and create emailroutes.js file .Then add the following code for it.

const router=require('express').Router();const mongoose=require('mongoose');const nodemailer=require('nodemailer');const sendgridTransport =require('nodemailer-sendgrid-transport');const transporter = nodemailer.createTransport(sendgridTransport({auth:{api_key: "Paste your API key here"
}
}))
router.route("/sendmail").post((req,res)=>{const user = {email : req.body.email}transporter.sendMail({to:user.email,from:"noreply.dswebsite@gmail.com",subject:"This is from Testing",text: "No reply \n" +"Hello "+ `${user.email} ` + "\n"+"Thank you",},()=>{res.json("true")})})module.exports = router;

you have to give API key as your api key which we saved in the begining.

you can refer my vs code given in below :

Sendemail.js

now you have to add the follwing code to the server.js :

const EmailRouter = require("./routes/Sendemail.js");app.use("/user",EmailRouter);

you can refer my vs code given in below :

server.js updated code

ok that’s all for the backend.now you can test this API route using postman or any other software.

let’s we going to test this API using postman:

postman output

you can see after send the request , I got json respond as “true”. It indicate that email was sent .Let’s check my gmail.

Gmail

you can see the email was received .

This is the end of the tutorial.you can use this for MERN stack project of your futher developments.

I hope you learned something.

Thank you for reading!!!….

--

--