{"id":25308,"date":"2022-03-31T11:38:24","date_gmt":"2022-03-31T11:38:24","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/blog\/?p=25308"},"modified":"2024-06-04T05:34:53","modified_gmt":"2024-06-04T05:34:53","slug":"send-mails-using-sendgrid-in-nodejs-app","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/blog\/send-mails-using-sendgrid-in-nodejs-app","title":{"rendered":"How to Set Up and Send Mails Using SendGrid in NodeJs App?"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>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\u2019t worry! We have this covered! <\/p>\n<p>In this tutorial: How to Set Up and <b>Send Mails using SendGrid in NodeJs App<\/b>, we will answer your questions and discuss them thoroughly! Trust me and read the blog till the end to know SendGrid inside out!<\/p>\n<p>Also, many developers use a package named NodeMailer to send emails. You can also visit the blog: <a href=\"https:\/\/www.bacancytechnology.com\/blog\/send-email-using-nodemailer\" target=\"_blank\" rel=\"noopener\">How to Send Email using NodeMailer with Gmail &#038; Mailtrap<\/a>, to learn more about NodeMailer.  <\/p>\n<p>Let\u2019s move on then!<\/p>\n<h2>Tutorial Goal: Set Up and Send Mails using SendGrid in NodeJs App<\/h2>\n<p>Here are your questions and queries! Let\u2019s have an overview of what our blog has for you, so you will continue reading till the end!<\/p>\n<ul class=\"bullets text-left\">\n<li>I\u2019m new to SendGrid: What is SendGrid?<\/li>\n<li>People appreciating SendGrid: Why use only SendGrid when you have different providers?<\/li>\n<li>I\u2019m a technical person. I want to set up and start using SendGrid with my Node App: Steps to set up and send emails using SendGrid in NodeJs app.<\/li>\n<\/ul>\n<p>Voila! Let\u2019s go!<\/p>\n<p class=\"boxed bg--secondary\" style=\"border: 1px solid #c7c7c7; box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);\"><strong><i><span style=\"font-size:22px; color:#000;\">Our clients say Bacancy provides the best and highly-skilled developers!<\/span><br \/>\nWant the best? Get the best! Contact Bacancy and <a href=\"https:\/\/www.bacancytechnology.com\/hire-node-developer\" target=\"_blank\" rel=\"noopener\">hire Node.js developer<\/a> for your dream projects!<\/i><\/strong><\/p>\n<h2>What is SendGrid?<\/h2>\n<p>SendGrid is a provider that allows SMTP (Simple Mail Transfer Protocol) service. I hope you\u2019re aware of SMTP. It reduces your time and efforts and provides flexibility when sending large volumes of emails. <\/p>\n<h2>Why use SendGrid in NodeJS to Send Emails?<\/h2>\n<p>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.  <\/p>\n<ul class=\"bullets text-left\">\n<li>Powerful email solution for sending bulk emails with high deliverability rates<\/li>\n<li>Provides interactive and eye-catching built-in email templates and freedom to edit them as well<\/li>\n<li>With the help of SendGrid, you can track the real-time performance of emails using various metrics like bounce rates, unsubscribers, delivered emails rate, unique reads\/opens\/clicks, and many more.<\/li>\n<li>Exceptional customer support<\/li>\n<li>Sendgrid has been integrated with so many tools that it lets you centralize the marketing efforts. The API integration with the various tools is very powerful.<\/li>\n<\/ul>\n<h2>Initial Project Setup<\/h2>\n<p>Create a NodeJs application using the below command.<\/p>\n<pre>mkdir SendgridApp\r\ncd SendgridApp<\/pre>\n<p><strong>Initialize Project<\/strong><\/p>\n<pre>npm init -y<\/pre>\n<p>It will create a <i>package.json<\/i> file.<\/p>\n<p>We will install the required dependencies in our project using the below command.<\/p>\n<pre>npm i express @sendgrid\/mail dotenv<\/pre>\n<p>Open the root file  of our project and name it as your will.<\/p>\n<h3>\/\/ app.js<\/h3>\n<pre>const express = require(\"express\");\r\nrequire(\"dotenv\").config();\r\nvar app = express();\r\napp.use(express.json())\r\nconst mailRoute = require('.\/routes\/sendMail')\r\napp.use(mailRoute)\r\napp.listen(process.env.PORT, console.log('Server is up and running '+ process.env.PORT))<\/pre>\n<h2>Set Up SendGrid Account<\/h2>\n<p>Let&#8217;s start with integrating Sendgrid.<\/p>\n<p>For setting up the SendGrid account, follow these instructions-<\/p>\n<ul class=\"bullets text-left\">\n<li>Visit <a href=\"https:\/\/sendgrid.com\/\" target=\"_blank\" rel=\"noopener\"><strong>SendGrid.com<\/strong><\/a><\/li>\n<li>Create an account<\/li>\n<li>Choose a plan according to your requirements<\/li>\n<li>Generate API key<\/li>\n<\/ul>\n<p>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.<\/p>\n<h3>\/\/ email\/account.js<\/h3>\n<pre>const sgMail = require('@sendgrid\/mail')\r\nrequire('dotenv').config()\r\nsgMail.setApiKey(process.env.SENDGRID_API_KEY) \/\/your sendgrid api key\r\n\r\nconst sendMail = (email, name) => {\r\n    sgMail.send({\r\n        To: email, \/\/ receiver email address\r\n        from: 'fromemail@email.com', \r\n        subject: 'here comes subject line', \r\n        text: `here comes the body ${name}` \r\n    })\r\n}\r\n\r\nmodule.exports = {\r\n    sendMail\r\n}\r\n<\/pre>\n<h2>Send Mails using SendGrid in NodeJs App<\/h2>\n<p>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.<\/p>\n<h3>\/\/ sendMail.js<\/h3>\n<p>For this example, we are sending mail for a specified route and call sendMail functions as per our requirement.<\/p>\n<pre>const express = require('express')\r\nconst { append } = require('express\/lib\/response')\r\nconst { sendMail } = require('..\/emails\/accounts')\r\nconst statusCode = require('..\/constants\/constants')\r\n\r\nconst router = new express.Router()\r\n\r\nrouter.get('\/sendmail', (req, res) => {\r\nconst user = req.body;\r\n    try {\r\n        sendMail(user.email,user.name)\r\n        res.status(statusCode.ok).send({message: 'Mail Sent'})\r\n    } catch (error) {\r\n        res.status(statusCode.internalServerError).send({error})\r\n    }\r\n})\r\n\r\nmodule.exports = router\r\n<\/pre>\n<p>As for this example, we have manually set the email or other required parameters that we can send through postman.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/03\/manually-set-the-email-min-1.png\" alt=\"manually set the email\" width=\"1100\" height=\"668\" class=\"aligncenter size-full wp-image-25330\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/03\/manually-set-the-email-min-1.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/03\/manually-set-the-email-min-1-300x182.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/03\/manually-set-the-email-min-1-1024x622.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/03\/manually-set-the-email-min-1-768x466.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/03\/Email-is-sent-successfully-min.png\" alt=\"Email is sent successfully\" width=\"1100\" height=\"616\" class=\"aligncenter size-full wp-image-25327\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/03\/Email-is-sent-successfully-min.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/03\/Email-is-sent-successfully-min-300x168.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/03\/Email-is-sent-successfully-min-1024x573.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/03\/Email-is-sent-successfully-min-768x430.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p>Bingo, we sent mail successfully! <\/p>\n<h2>Conclusion<\/h2>\n<p>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\u2019t waste your time and start learning more about NodeJS technology! Are you looking for reliable company to build your application? Contact Bacancy- the best <a href=\"https:\/\/www.bacancytechnology.com\/node-js-development\" target=\"_blank\" rel=\"noopener\">Node.js development company<\/a> and start working with us!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction 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\u2019t worry! We have this covered! In this tutorial: How to Set Up and Send Mails using SendGrid in NodeJs App, [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":25338,"comment_status":"open","ping_status":"open","sticky":false,"template":"blog-new-template.php","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[483],"tags":[],"coauthors":[1585],"class_list":["post-25308","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js"],"acf":[],"modified_by":"Chandresh Patel","_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/25308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=25308"}],"version-history":[{"count":0,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/25308\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media\/25338"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=25308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=25308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=25308"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/coauthors?post=25308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}