DEV Community

Cover image for How To Send Email Using SendGrid In Node.js Application
Pankaj Kumar
Pankaj Kumar

Posted on

How To Send Email Using SendGrid In Node.js Application

In this article, I will explain how to send email using sendgrid in nodejs application. Sending email using SendGrid is very easy with nodejs express, Because there is a npm package available for performing the task which makes the task very simple. We will use npm package @sendgrid/mail.

Here I will use jade/pugjs as a nodejs template engine.

At first create the package.json file and put the below code inside it.

{
"name": "sending-emails-using-sendgrid-in-nodejs",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": [
],
"author": "Suraj Roy",
"license": "ISC",
"dependencies": {
"express": "^4.14.0",
"pug": "^2.0.3",
"@sendgrid/mail": "^6.3.1",
}
}
Enter fullscreen mode Exit fullscreen mode

Saving the above file, run the npm install command to install the mentioned npm package. After creating the above file we have another main file called server.js file.


// call the needed packages
const express = require('express');
const mailer = require('@sendgrid/mail');
const app = express();

mailer.setApiKey('yourSendGridKey');

const port = process.env.PORT || 3000; // set our port

// set the view folder to views
app.set('views', __dirname + '/views');
// set the view engine to pug
app.set('view engine', 'pug');


app.get('/', function (req, res) {
sendEmail({
toAddress: 'demo.jsonworld@gmail.com',
subject: 'Email from SMTP sever',
data: {
name: 'Json World',
message: 'Hello there!'
},
htmlPath: "home.pug"
}).then(() => {
return res.send('Email has been sent!');
}).catch((error) => {
return res.send('There was an error while sending the email');
})
});

const sendEmail = function(mailOptionsObject) {

const html = pug.renderFile(
__dirname + "/../views/email/" + mailOptionsObject.htmlPath,
mailOptionsObject.data
);

const msg = {
to: mailOptionsObject.toAddress,
from: config.get('emailFrom'),
subject: mailOptionsObject.subject,
html: html
};

const status = sgMail.send(msg)
return status;

};

app.listen(port, function () {
console.log(`Server is running over port: ${port}!`);
});

Enter fullscreen mode Exit fullscreen mode

After creating the server file, Next task is to create the view part. Lets create a folder name view and then a file name home.pug. Put the below code inside home.pug


doctype html
head
body
div
p
b Hi #{user.name}
div
p #{user.message}

Enter fullscreen mode Exit fullscreen mode

After completing all the above stuff, run the app with npm start and check the app over browser running the app over the link localhost:3000.

Conclusion

So in this article, We learn how to send email using SendGrid in nodejs application.

That’s all for now. Thank you for reading and I hope this article will be very helpful to understand how to send email using SendGrid in nodejs application .

Let me know your thoughts over the email demo.jsonworld@gmail.com. I would love to hear them and If you like this article, share with your friends.

This article is originally posted over jsonworld

Top comments (0)