The Talent500 Blog
How to Set Up and Send Mails Using SendGrid in NodeJs App? 1

How to Set Up and Send Mails Using SendGrid in NodeJs App?

Introduction

Email is an essential part of modern communication, and it’s crucial to have a reliable email delivery system in your Node.js app. SendGrid is a cloud-based email delivery service that allows you to send emails from your app. It provides a simple and efficient way to send emails without worrying about email server setup, email throttling, or email blacklist issues. In this guide, we will walk you through the steps to set up and send emails using SendGrid in your Node.js app, we will cover everything you need to know to get started with SendGrid.

Set Up and Send Mails Using SendGrid in NodeJs App

What is SendGrid?

SendGrid is an SMTP (Simple Mail Transmission Protocol) service provider. We trust you’re familiar with SMTP. It saves your time and effort while also giving you more freedom when sending large amounts of emails.

Why use SendGrid in NodeJS to Send Emails?

Below are the reasons why you should use SendGrid for sending emails.

  • A powerful email solution for delivering large amounts of email with good deliverability rates.
  • Offers interactive and eye-catching built-in email templates, as well as the ability to alter them.
  • SendGrid allows you to track the real-time performance of emails using data like bounce rates, unsubscribers, delivered emails rate, unique reads/opens/clicks, and many more.
  • Excellent customer service
  • Sendgrid is linked with so many tools that you can consolidate your marketing efforts. API integration with many tools is really strong.

A: Initial Project Setup

1.Using the below command, create a NodeJs application.

mkdir SendgridApp

cd SendgridApp

 

2.Initializing the Project
npm init -y


3.This creates a package.json file.

Using the below command, install the required dependencies in our project.

npm i express @sendgrid/mail dotenv

Open the root file of your project and name it as you wish.

// app.js

const express = require(“express”);

require(“dotenv”).config();

var app = express();

app.use(express.json())

const mailRoute = require(‘./routes/sendMail’)

app.use(mailRoute)

app.listen(process.env.PORT, console.log(‘Server is up and running ‘+ process.env.PORT))

 

B: Setting Up SendGrid Account

Let’s start with integrating Sendgrid.

Follow these instructions to set up the SendGrid account, –

  1. Go to SendGrid.com.
  2. Establish a user account
  3. Pick a plan that meets your needs.
  4. Create an API key

We must configure the SendGrid API key in the application before we can send emails from the NodeJS app. SendGrid allows you to create an email based on your needs. We can also add and change HTML, photos, documents, and so on.

// email/account.js

const sgMail = require(‘@sendgrid/mail’)

require(‘dotenv’).config()

sgMail.setApiKey(process.env.SENDGRID_API_KEY) //your sendgrid api key

 

const sendMail = (email, name) => {

    sgMail.send({

        To: email, // receiver email address

        from: ‘fromemail@email.com’, 

        subject: ‘here comes subject line’, 

        text: `here comes the body ${name}` 

    })

}

 

module.exports = {

    sendMail

}


C: Send Mails using SendGrid in NodeJs App

We may utilise the sendgrid method anywhere we need, such as when a user registers or leaves, or for other alerts.

// sendMail.js

In this example, we are sending mail for a certain route and using sendMail methods as needed.

const express = require(‘express’)

const { append } = require(‘express/lib/response’)

const { sendMail } = require(‘../emails/accounts’)

const statusCode = require(‘../constants/constants’)

 

const router = new express.Router()

 

router.get(‘/sendmail’, (req, res) => {

const user = req.body;

    try {

        sendMail(user.email,user.name)

        res.status(statusCode.ok).send({message: ‘Mail Sent’})

    } catch (error) {

        res.status(statusCode.internalServerError).send({error})

    }

})

 

module.exports = router


In this case, we manually specify the email or other essential parameters that we may send using Postman.

Conclusion

In this blog, we learned how to send emails using Node.js using SendGrid. We can send various transactional emails, including the order confirmation. Sending emails using Node.js and SendGrid becomes a snap if the email system is properly configured and templates are dynamic.

Setting up and sending emails using SendGrid in a Node.js app is a straightforward process that can be accomplished with just a few steps. By registering for a free SendGrid account, creating an API key, and installing the necessary packages, you can start sending emails from your app using SendGrid’s reliable email delivery system. With the help of the tutorials and code examples mentioned in this guide, you can easily integrate SendGrid into your Node.js app and start sending transactional emails. We encourage you to explore more Node.js tutorials and resources to further enhance your skills and knowledge. 

 

0
Afreen Khalfe

Afreen Khalfe

A professional writer and graphic design expert. She loves writing about technology trends, web development, coding, and much more. A strong lady who loves to sit around nature and hear nature’s sound.

Add comment