For every every web application, there is a need to send the user an email. In this article, I would love to explin you how easy it is to setup Sendgrid with Node.Js.
Node Setup
npm init -y
Installing SendGrid
npm install @sendgrid/mail
Install dotenv from npm
You want to read the .env file and the dotenv package on npm does this very well. Install the dotenv package by running the following command
npm install dotenv
Set Sendgrid API Key in .env file
SendgridAPIKey = SG.XXXXX.XXXXX.XXXXX
Read the .env file
It’s time to read the .env file with a little bit of code. Replace the contents of your index.js file with the following code.
const dotenv = require('dotenv');
dotenv.config();
Import Sendgrid class to send Email
It’s time to read the .env file with a little bit of code. Replace the contents of your index.js file with the following code.
const sendGridMail = require('@sendgrid/mail');
sendGridMail.setApiKey(`${process.env.SendgridAPIKey}`);
Create one function to Get Email Body Message
function getMessage() {
const body = 'This is a test email using SendGrid from Node.js';
return {
to: 'abc@gmail.com',
from: 'xyz@gmail.com',
subject: 'Test email with Node.js and SendGrid',
text: body,
html: `<strong>${body}</strong>`,
};
}
Create one function for Sending Email
async function sendEmail() {
try {
await sendGridMail.send(getMessage());
console.log('Test email sent successfully');
} catch (error) {
console.error('Error sending test email');
console.error(error);
if (error.response) {
console.error(error.response.body)
}
}
}
Initiate Send Email Function
sendEmail();
I hope you liked the article, you can use this Repo for sending Email using Sendgrid API. You can create your own function to scale this boilerplate