Contact information

1303, Shivalik Shilp, Iskcon Cross Rd, Ahmedabad 380015, INDIA

We are available 24/ 7. Call Now. +91 99741 86581 services@jarvisbitz.com
  • circle
  • circle
  • circle
How to Install and Use SendGrid in a NodeJs App to Send Emails?

Introduction

You are already aware of SendGrid’s goal and the significance of sending mail if you have found this blog. Consider providing answers to such queries as What is SendGrid and Why Use SendGrid. Not to worry! This is covered by us!

We’ll address all of your inquiries in this article on how to set up and send emails using SendGrid in a Node.js application. To fully understand SendGrid, I urge you to read this blog post through to the end.

Additionally, a lot of developers utilize the NodeMailer package to send emails. For more information on NodeMailer, see the blog post How to Send Email using NodeMailer with Gmail & Mailtrap.

What is SendGrid?

An SMTP (Simple Mail Transfer Protocol) service provider is SendGrid. Hope you’re familiar with SMTP. When sending a lot of emails, it gives you flexibility and saves you time and work.

Why use SendGrid to send emails with Node.js?

Node.js included, you can send emails in a variety of methods from any computer language. You might use a software like Nodemailer, for instance, which supports SMTP, Sendmail, and even Amazon SES in addition to other transports.

 

However, the issue with using an arbitrary SMTP or Sendmail is that the emails would probably be marked as spam and sent to our clients’ rubbish boxes. And that is something that we firmly oppose.

 

However, for our needs, they provide a free subscription that still enables us to send up to 100 emails daily. They offer crucial features like statistics and a template editor in addition to better delivery.

Set Up SendGrid Account

Sendgrid integration should go first.

 

Follow these guidelines to set up your SendGrid account:

 

  • Check out SendGrid.com
  • Create a profile
  • Choose a strategy based on your needs.
  • Create an API key.

 

You will be prompted to enable two-factor authentication when you logout SendGrid and login. Your mobile number and SMS are required for setup.

 

We’ll then see how to quickly test sending emails using Node.js and the SendGrid API key after this is finished.

Set up a simple Node.js email script.

You need to run the following commands with npm to install a new Node.js application:

npm init -y

After that, install the @sendgrid/mail npm package:

npm i –save @sendgrid/mail

After that, make a file called index.js to send mail and add the following code to it:

const nodemailer = require(‘nodemailer’);

const config = require(‘../config’);

let transporter = nodemailer.createTransport({

 host: ‘smtp.sendgrid.net’,

 port: 587,

 auth: {

   user: “apikey”,

   pass: config.SENDGRID_API_KEY

 }

})

This code will import the configuration file and install nodemailer. Then, using the specified server host, port number, and authentication information, it will establish a transporter.

 

The suggested port for SendGrid’s SMTP relay server is 587, and the server host is smtp.sendgrid.net.

 

You must enter a username and password for the authentication data. The login and password for connecting to SendGrid’s SMTP server are “apikey” and your actual API key, respectively. SendGrid will connect with your SendGrid account using the provided API key, allowing you to view and track sent emails.

 

After that, Add the following code below the transporter:

const mailOptions = {

 from: config.fromEmail, // sender address

 to: data.to, // list of receivers

 subject: ‘Reset Password Request’, // Subject line

 html: `<div>Please active your account</div>` // html body

};

// send mail with defined transport object

await transporter.sendMail(mailOptions, (error) => {

 if (error) {

   callback(false);

 } else {

   callback(true);

 }

});

The transporter is used in the final section of code to send an email with a specific message setting. with a specified transport object, send a mail.

 

In the case of this example, we manually set the email and any other necessary parameters that you may send using Postman.

Conclusion

This article taught us how to set up and send emails using SendGrid in a NodeJs app. We also looked at other email-sending options, like notification settings and delivery time estimation. So you can see how simple SendGrid makes it to send emails. It greatly improves productivity while saving you time.

Yes, you ought now be able to create your own SendGrid account by now and start sending emails right away!