How To Notify Admin After A Form Submission With Node.js?

The main idea of this article is to explain how to use Nodemailer to notify the Admin for every form that been submitted on the particular website. We’ll focus on HTML aspects and also do an overview of all Nodemailer capabilities.

Besides that, this tutorial will help you prepare and test email messages to send out with your Nodejs application.

With Nodemailer module, now it is possible to notify the Admin with a “Form submission details” message in Nodejs and Express.js. Before going into the topic, let's have a basic idea of Nodemailer module and its relevant functionalities.

What is Nodemailer?

Nodemailer is a module with zero dependencies for Nodejs applications. It is specially designed for sending emails by integrating with any other email service provider. The important features of Nodemailer are:

  • Platform-independence
  • Highly secured delivery system with DKIM email authentication
  • Unicode support
  • HTML content and embedded image attachments

Let’s learn how to carry the process in a step-by-step manner, starting from creating an email template,

Step 1

First, start with creating an email template with ejs(email/contact.ejs), where all the contents and information are orderly placed. Before creating an email template, one should plan for different fields to be included in the template. A standard email template contains the following dynamic fields,

  • Name
  • Email address
  • Phone/Contact number, and
  • Custom message

Step 2:

Create a form using ejs for the appropriate web page where you need to place the form. Now the form that been created is stored in the database. You can’t simply pull the form data form the database, for that we need to route the particular form by routing. So route the particular web page by routing the file.

Now routing the web page is completed. And one should be aware that Nodejs has its own limitation of require routing file for each web pages to get the URLs.

Sample code:

<form method=”post” action=”thank-you”>

<div class=”form-group”>

<input type=”text” name=”name” class=”form-control” placeholder=”Name” required>

</div>

<div class=”form-group”>

<input type=”email” name=”email” class=”form-control” placeholder=”Email” required>

</div>

<div class=”form-group”>

<input type=”tel” name=”phone_no” class=”form-control” placeholder=”Phone Number” required>

</div>

<div class=”form-group”>

<textarea name=”message” class=”form-control” placeholder=”Message” required rows=”4"></textarea>

</div>

<div class=”text-center”>

<button type=”submit” class=”btn btn-dark px-5 py-3 rounded”><b>Submit</b></button>

</div>

</form>

Step 3:

Now it’s time to install and configure the “NODEMAILER” package. As we said before, Nodemailer is a simple module but yet a powerful email-module to send an email by configuring the package requirements.

The above step involves two processes to carry out:

INSTALL

npm install nodemailer

CONFIGURE

Now configure the nodemailer on the index.js file as shown below:

var “nodemailer = require(“nodemailer”);”

Step 4:

After installing and configuring the nodemailer in your node.js file require the nodemailer.

var nodemailer = require(‘nodemailer’);

Every nodemailer needs some sort of transport service. With which it can send emails through the server. This transport service can only be done by setting up a transporter. So lets set up a transporter as shown below:

var admin_mail=”info@yourdomain.com”;

var transporter = nodemailer.createTransport({

host: ‘mail.yourdomain.com’,

port: 465,

secure: true,

auth: {

user: ‘info@yourdomain.com’,

pass: ‘password’

}

});

Now the transporter is all set to perform transferring the mail using nodemailer.

Step 5:

Declare the submission values in the transporter. Then connect the data with mail option along with an email template which you have created before using the ejs.renderFile.

We have illustrated the above step with a sample code example:

router.post(‘/thank-you’, function(req,res,next){

var data=req.body;

var name=data.name;

var email=data.email;

var phone_no=data.phone_no;

var message=data.message;

ejs.renderFile(__dirname + “/email/contact.ejs”, { ‘name’:name,’email’:email,’phone_no’:phone_no,’message’:message }, function (err, data) {

if (err) {

console.log(err);

} else {

var mainOptions = {

from: email,

to: admin_mail,

subject: ‘Request Demo Form Submission’,

html: data

};

transporter.sendMail(mainOptions, function (err, info) {

if (err) {

console.log(err);

} else {

//console.log(info);

console.log(‘Message sent: ‘ + info.response);

}

});

}

});

});

Now you’re all set to go-ahead to test the form submission, and see whether the Admin getting notified or not. If yes, have a great email configuration. If not, let us know by commenting on the issue that you’re facing.

I Hope, this knowledge base will help you to learn and fix any Nodemailer and Form submission related issues.

Final Note

We have reviewed how to notify admin after the form submission on the website with Nodejs and Expressjs. And also how to create and send an email template in Nodemailer with different types of contents such as HTML, tables, descriptions, attachments, and embedded images.

Nodemailer has some tremendous capabilities and offers a lot of options and settings to configure and customize each and every part of your email. With its advanced options, it extends its capabilities with other parameters like email templates, mailer classes, utilities, loggers, and helpers to send authenticated emails using Gmail or any mail services.

Want to know more about the Nodejs and its relevant technologies, Contact our Nodejs experts today.

Written by

Ecommerce consultant

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store