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
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 nodemailer
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/");
- });
Zash AshleyPosted Dec 27, 2017, 6:58 AM
Mail not send ...stop at sending email please wait