How to Send Emails With Templates using NodeJs

Purti Aggarwal
jsblend
Published in
4 min readJun 3, 2021

--

In this technology world, email is the most tool which you to reach your users. This article will give you brief how-to send emails with attractive templates using the server NodeJs. We are going to use the nodemailer library for sending mail. Nodemailer is a library that allows us to send emails through the mails. We can send emails when we want to communicate with the client-side users or notify developers when something went wrong.

Nodemailer is the module for NodeJs application which allow sending emails as easy as sending cake.

We are going to use handlebars for designing our emails.

Handlebar is a template engine to make writing html code easier.

Let’s do it practically

Step:1 Project Structue

We will make the index.js file which contains the server code of the project inside the src directory. The src directories also have an image directory and the templates directory. The image directory will have static images whereas the templates directory will have templates that we will send through the emails, partials directory has snippets that can be shared between pages, such as header and footer, and views directory contain the pages. Later on, 3 more directories will be added named node_module, package-lock.json, and package.json which have the data of installed modules.

LICENSE, .gitignore, and README.md are the git files.

Check out my previous blog on how to setup a project from scratch.

Directory Structure

Step:2 Installing the Modules

We need to set up the package.json file by running the below command in the terminal inside the application directory:

$ npm init -y

npm init -y command will take default configuration like description, name and other config.

If you want to provide custom configuration use npm init and it will ask you to enter all the details manually.

Bash File

Now Install the required Modules

$ npm i express nodemailer nodemailer-express-handlebars
  1. Express: Express is used as a web application framework.
  2. Nodemailer-express-handlebars: Nodemailer-express-handlebars is a plugin for nodemailer for the use of express-handlebars.
  3. Nodemailer: Nodemailer is the module for the NodeJs application that allows sending emails as easily as sending a cake.
Package.json

Step:3 Setup Server File

Let’s start with the server file, Server file is the main file of our project that is index.js. The index.js file has the code for setting the server. Once the server up and running, the user can able to hit API’s.

The app.listen() function used to bind and listen to the connection on the specified host and port.

Step:4 Config Gmail Account

For sending the email we are using a Gmail account which means we are using Gmail as our transport service. So, now our next step is to tell nodemailer to login into our account and send emails on our behalf. This can be done by using nodemailer.transport() function.

Next, we need to configure the data which we have to use for sending mail inside the sendMail function.

These are important options for sending an email with a designing template.

  1. Form: Here we have to mention the sender’s email id.
  2. To: Here we give the receiver's email id.
  3. Subject: Here we give the subject of the email.
  4. Template: Here we give the template name which template we want to send through the email.
  5. Attachment: Here we mention the fileName and the path of that file which we want to send as an attachment.

Step:5 Create template

Now we create an email template but first, we have to allow nodemail to compile the handlebars. For this, we have a middleware transport.use()

Now, we moving ahead for creating a template. First, we code for the partials. Partials have a small part of code that can be used in the template pages.

First, we create header.handlebars

Now, we code for footer.handlebars

The last step is to create a page for the template inside ‘templates/views/index.handlebars’.

Step:6 Hit API

Our last step is to create an API for sending emails.

Now it’s time to test our work.

Result of API

Conclusion

In this blog, we have seen how to send emails from the server and we used the Gmail service to send the email. We also learned how to use handlebars to send templates. To send an email we use the nodemailer module. Nodemailer helps us to send emails easily. Handlebars are the view engine for the code HTML easily on the server-side.

Hopefully, this helps you with sending emails using NodeJs.

You can find the above code [here].

--

--