Introduction
In this article, we will learn how to send an email in node.js. As you know, sending emails in any application is an important function. First, I will setup node.js application using NPM commands; then I will implement email function. To implement the main function, there are several packages available. We will use "nodemailer" package. We will install this package using the following npm command
npm install nodemailer
Background
You should have a basic understanding of the node.js application, JavaScript, and HTML.
Code implementation
Requirement
- Node.js version should be greater than 6.0
- Express.js
- One text editor like Sublime or any other
Create Node.js Application
First, we will create a package.json file which is very important and contains information of all the configuration and packages included in our application. So, let's create package.json file using command "npm init". Open terminal and type command "npm init". It will ask to enter all the details about the configuration.
After entering these details, it will create configuration file automatically in your application folder. The configuration file will contain the information regarding project configuration.
Now, we have to install Express with the latest version. To install the Express.js framework, we will execute the npm command -
npm install express
It will install the latest version of Express. Ater Express is installed, we will install "nodemailer" using -
npm install nodemailer
Nodemailer will be used to send the email in node.js. To send the email in node.js, there are many packages available but we will use nodemailer package.
After the installation of all these packages, the package.json file will look like the following.
- {
- "name": "send-main-using-nodejs",
- "version": "1.0.0",
- "description": "we will send mail using nodejs with gmail authentication",
- "main": "server.js",
- "scripts": {
- "test": "echo \"Error: no test specified\" && exit 1"
- },
- "author": "Jyoti",
- "license": "ISC",
- "dependencies": {
- "nodemailer": "^4.0.1",
- "express": "^4.8.0"
- }
- }
In the above package.json file, we can see the installed packages information like Express having the version 4.8.0 and nodemailer having the version 4.0.1. If you look at the main script in the package.json file, you will notice that command that the start of the application is "main": "server.js". To start the application, you will have to execute the script "node server.js"
Now, we will create a new file named "server.js" in our application and will include all the required files. In the server.js file, we will call the references to Express and nodemailer.
Now, you can see that we have created the smtptransport object from nodemailer. It will authenticate and send the mail. Now, you have to pass the one email id and password of Gmail. It will be used to authenticate and then send the email to the recipients.
Find the below server.js file.
- var express = require('express');
- var nodemailer = require("nodemailer");
- var app = express();
-
- var smtpTransport = nodemailer.createTransport({
- service: "gmail",
- host: "smtp.gmail.com",
- auth: {
- user: "gmailemailId",
- pass: "password"
- }
- });
-
- app.get('/', function(req, res) {
- res.sendfile('index.html');
- });
-
- app.get('/sendmail', function(req, res) {
- var mailOptions = {
- to:"toemalid",
- subject:"Email from nodemailer",
- html:'<div>Name: '+ req.query.name +'</div><div>Email: '+ req.query.email +'</div><div>Mobile: '+ req.query.mobile +'</div><div>Message: '+ req.query.message +'</div>'
- }
- smtpTransport.sendMail(mailOptions, function(error, response) {
- if(error) {
- res.end("error");
- } else {
- res.end("sent");
- }
- });
- });
-
- app.listen(3000, function() {
- console.log("Application started on http://localhost:3000/");
- });
Now, create the HTML page which contains a contact form.
- <html>
- <head>
- <title>Send Message in Node.js</title>
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
- </head>
- <body>
- <div id="container">
- <h1>Send Main Function In Node.JS</h1>
- <table>
- <tr>
- <td>Name: </td>
- <td><input type="text" name="txtname" id="txtname" placeholder="Enter Name"></td>
- </tr>
- <tr>
- <td>Email: </td>
- <td><input type="text" name="txtemail" id="txtemail" placeholder="Enter Email"></td>
- </tr>
- <tr>
- <td>Mobile: </td>
- <td><input type="text" name="txtmobile" id="txtmobile" placeholder="Enter Mobile"></td>
- </tr>
- <tr>
- <td>Message: </td>
- <td>
- <textarea id="txtmessage" name="txtmessage" cols="40" rows="5" placeholder="Enter Message"></textarea>
- </td>
- </tr>
- <tr>
- <td> </td>
- <td>
- <button id="btnsendemail">Send</button>
- </td>
- </tr>
- <tr>
- <td></td>
- <td>
- <span id="message"></span>
- </td>
- </tr>
- </table>
- </div>
- <script>
- $(document).ready(function(){
- var name;
- var email;
- var mobile;
- var message;
- $("#btnsendemail").click(function() {
- name = $("#txtname").val();
- email = $("#txtemail").val();
- mobile = $("#txtmobile").val();
- message = $("#txtmessage").val();
- $("#message").text("Sending Email please wait ...");
- $.get("/sendmail",
- {
- name: name,
- email: email,
- mobile: mobile,
- message: message
- },
- function(data) {
- if(data == "sent") {
- $("#message").html("Email has been send successfully.");
- }
- });
- });
- });
- </script>
- </body>
To run this project, enter "node server.js" in terminal.
Then, type "localhost:3000" in any browser followed by an Enter.
The page will open; fill in the form and click the button to send mail. Don't forget to enter the GMail address and password for authentication.