If you have landed on this blog, you know the purpose of SendGrid and the importance of sending mail! You might want to answer questions like What is SendGrid, and Why use SendGrid? Don’t worry! We have this covered!
In this tutorial: How to Set Up and Send Mails using SendGrid in NodeJs App, we will answer your questions and discuss them thoroughly! Trust me and read the blog till the end to know SendGrid inside out!
Also, many developers use a package named NodeMailer to send emails. You can also visit the blog: How to Send Email using NodeMailer with Gmail & Mailtrap, to learn more about NodeMailer.
Let’s move on then!
Here are your questions and queries! Let’s have an overview of what our blog has for you, so you will continue reading till the end!
Voila! Let’s go!
Our clients say Bacancy provides the best and highly-skilled developers!
Want the best? Get the best! Contact Bacancy and hire Node.js developer for your dream projects!
SendGrid is a provider that allows SMTP (Simple Mail Transfer Protocol) service. I hope you’re aware of SMTP. It reduces your time and efforts and provides flexibility when sending large volumes of emails.
Coming back to our next question, Why SendGrid with NodeJS? No beating around the bush, and here are the reasons why you should use SendGrid for sending emails.
Create a NodeJs application using the below command.
Initialize Project
It will create a package.json file.
We will install the required dependencies in our project using the below command.
Open the root file of our project and name it as your will.
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))
Let’s start with integrating Sendgrid.
For setting up the SendGrid account, follow these instructions-
For sending emails from our NodeJS app, we need to configure the SendGrid API key in the application. The SendGrid gives you the freedom to set up an email as per your requirement. We can also add and modify HTML, images, documents, etc.
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: '[email protected]', subject: 'here comes subject line', text: `here comes the body ${name}` }) } module.exports = { sendMail }
We can use the sendgrid function wherever we want to use it, for example, when a user is registering or leaving or for some other notifications.
For this example, we are sending mail for a specified route and call sendMail functions as per our requirement.
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
As for this example, we have manually set the email or other required parameters that we can send through postman.
Bingo, we sent mail successfully!
I hope the purpose of this tutorial: How to Set Up and Send Mails using SendGrid in NodeJS app, was useful to you. If you have any questions or suggestions, please contact us and share your thoughts! If you are a NodeJs enthusiast and willing to polish your knowledge, then the NodeJS tutorials page is for you! Don’t waste your time and start learning more about NodeJS technology! Are you looking for reliable company to build your application? Contact Bacancy- the best Node.js development company and start working with us!
Your Success Is Guaranteed !
We accelerate the release of digital product and guaranteed their success
We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.