Send Emails with NodeJS and NodeMailer

In this post, we are going to learn to Send Emails with NodeJS and NodeMailer library. Email is one of the commonly used tools for communication in web applications because it helps us to reach your users instantly, build your business and promotions, or send endless notifications.

There are several Node.js modules used for sending emails. In that, nodemailer is the most familiar option. This package has a module that gives you the ability to quickly send emails without bother. The Simple Mail Transfer Protocol is used for sending email between servers.

Step 1: Install nodeJs package and Init application

Initially download NodeJS and install it in your system and confirm everything configured correctly using the following command.

Step 2: Create a directory and init application

Create an application folder and init the project using the following command.

Step 3: Install required packages using NPM

The following modules are going to be required to create the application.

  • Express: Express is a least and adjustable Node.js web application framework that gives a strong collection of features for web and mobile applications. Express gives a light layer of basic web application benefits, without covering Node.js benefits that you agree and like.
  • body-parser: used to parse incoming requests from the end client.
  • ejs: is a templating engine and its used to render HTML pages to end client
  • NodeMailer: There are several Node.js modules used for sending emails. In that, nodemailer is the most familiar option. This package has a module that gives you the ability to quickly send emails without bother.
  • nodemon: Optional package and Installed globally. It helps us to listen for modifications to files and automatically restart the app server.

Run the following command to install the above mention modules as dependencies for our application.

Step 4: Create app.js file

Create app.js server file and include the required dependency package in it for our file upload application. And define the server port and write an app server .listen() function.

Create app.js file

include following code

Run app.js using following comment

Step 5: Define View engine with ejs / public path / View files path

Here we are going to define a template view engine with ejs using set() and use() function and also define a Public path and View folder path location.

Step 6: Define and configure ModeMailler and transporter details

We need to include the NodeMailer package into our app.js and define a service provider to send mail to various servers. NodeMailer requires a transport service utilizing which it can send emails. In this article, we are using the Gmail service to sent mail to various client servers. For defining transposer services we need to add the following code into the app.js file.

Step 7: Define index path with ‘/’ and HTML file

Here we are going to define the HTML page with ejs extension and adding HTML content in it. We created four HTML files for file upload operation. I am going to create a file with named as email.js

Index route Request handler

For Email From 

Step 8: Define Email form post function handler in app.js

Here we are going to define the email form handling and sending email functions using NodeMailer.

For sending HTML content in EMail,

We can able to sent Email with HTML content using “html” attribute. Instead of “text” attribute we can replace with “html” for this option.

For sending HTML content with Attachment in EMail,

We can able to sent Email with Attachments using “attachments” attribute. Attachments attribute have two arguments for file attachments. It have filename and path for file attachments.

Final and full Mail function setup for sending emails,

Step 9: Run a server and check with Browser

Here we are going to run an application with the following command to test our Send Emails with NodeJS and NodeMailer application in a browser.

Finally app.js file code following details.

Run the Application using the following command.

Now you are ready to see the demonstration of Send Emails with NodeJS and NodeMailer.

Download

 

Learn Infinity

Learn Infinity is the most famous Programming & Web Development blog. Our principal is to provide the best online tutorial on web development. We execute the best tutorials for web experts this will help developers, programmers, freelancers and proving free resource you can download or preview the tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top