Sending email notification is basic need of any web application. Whether it is used for Sending marketing emails or support email for ECommerce site. We have earlier see how to send an email using shiftMailer in Symfony framework. In this tutorial we will see how to send email in NodeJs using Nodemailer (Official Site). NodeJS is JavaScript framework and it have too many packages which allow you sending emails from your node application.



Send Email using Nodemailer in NodeJS and ExpressJS

Send email in Nodejs and express using nodemailer

We are going to use nodemailer package to send emails. Nodemailer is single module with zero dependencies sponsored by MoonMail. MoonMail is well-known email marketing software, it also offers free membership with limited functions which is best in the industry. It supports Unicode support, Windows support, HTML content as well as plain text, Embedded image attachment for HTML content, OAuth2 authentication, Proxies for SMTP connections and more features.

Required packages
  • express
  • nodemailer
Directory Structure

Package Installation

The first steps of development is to install packages. Install all required packages using npm by executing below command:

 

Read more about how to install package in node.
Create a RESTFul API Using Node.JS, Express.JS and MongoDB
How to Create Database in MongoDB

 

After installation of all the packages our package.json will look like:

Setup and Configuration

Setup express app and set Default port, Body parser to parse parameters. Finally create a server which listen specific port from your system. Make sure that the port you are using is not already used by other application.

Email transporter configuration

Our App is up to date, now set transporter configuration for sending email functionality.

Function nodemailer.createTransport() will create new transport that allows us to send email to single or multiple recipient.

Update connection information such as host (SMTP host), Port, Auth, Secure (SSL secure or not) in above code.

Create Route

Create route which allows POST HTTP method with three parameters such as email, subject and message.

Input Parameters

  • email
  • subject
  • message

You can send email to multiple recipient by adding coma separated email address in to. Fucntion transporter.sendMail() sends an email to specified email addresses and returns status code in response for each recipient.

User will pass To email, subject and message parameters from front end and pass them into POST request.

Set Form Validation

In this part, we will validate the user inputs. We will express-validator module to validate user inputs.

Apply input validations in route. Copy following and update route. request.getValidationResult().then() function validates if input parameters validated or not. If any input validation error found than it will return Error message, input field name and status as failure into response.

app.js (Full Code)

Server is ready not open command-line or temrinal and navigate to applications root directory. Now, start node server using below command:

Test application by sending request to http://localhost:8080/email/send URL using postman chrome extension. You can send request to the endpoint using jQuery, AngularJS or other JavaScript frameworks as well.

 

Read More:

Create RESTful API Using Node.JS, Express and MySQL

Create a RESTFul API Using Node.JS, Express.JS and MongoDB Database

Create a RESTful Web Service API with Slim

How to Create Database in MongoDB

Integrate PayUMoney Payment gateway in PHP

How To Integrate Stripe Payment Gateway Using PHP and JavaScript

sSwitch – JQuery Toggle Button Plugin For Sliding Toggle Switches

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">