How to send email in nodejs
Today we are going to learn how to send email in node.js, If we are developer then each phase of development we need to implement email functionality in our website.
So in this post you’ll se how to send email in nodejs with SMTP and without SMTP , there is very cool module “nodemailer” available in npm to send email with smtp.
You can learn more about this module form here http://www.nodemailer.com
Lets start step by step…
Make sure that you have already setup nodejs environment with express framework.

Step-1: Create package.json file in root directory of your application.
package.json
{
"name": "NodeJs-Emailer",
"version": "0.0.1",
"description": "NodeJs email form to send email using nodejs",
"dependencies": {
"nodemailer": "~0.7.0",
"express": "~3.4.0",
"body-parser": "~1.13.1"
}
}
Step-2: Create index.html file and paste below html code & save it.
index.html
Send email in nodejs
Email Form In Node.Js
Step-3: Create app.js file in your folder and paste below server side code in app.js file.
Here i am using Hotmial SMTP to send emails you can use other mail server smtp like Gmail, Yahoo as your need.
Note: Currently app.js configured for simple email without SMTP. If you want to use SMTP then remove comment from SMTP section in app.js try to run app.js again.
app.js
/*
* Author: Rohit Kumar
* Date: 24-06-2015
* Website: iamrohit.in
* App Name: Node Emailer
* Description: NodeJs script to send emails
*/
var http=require('http');
var express=require('express');
var nodemailer = require("nodemailer");
var bodyParser = require('body-parser')
var app=express();
var port = Number(process.env.PORT || 5000);
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({
extended: true
}));
// Home page
app.get('/',function(req,res){
res.sendfile('index.html');
});
// sending mail function
app.post('/send', function(req, res){
if(req.body.email == "" || req.body.subject == "") {
res.send("Error: Email & Subject should not blank");
return false;
}
// Sending Email Without SMTP
nodemailer.mail({
from: "Node Emailer ✔ ", // sender address
to: req.body.email, // list of receivers
subject: req.body.subject+" ✔", // Subject line
//text: "Hello world ✔", // plaintext body
html: ""+req.body.description+"" // html body
});
res.send("Email has been sent successfully");
// Sending Emails with SMTP, Configuring SMTP settings
/*var smtpTransport = nodemailer.createTransport("SMTP",{
host: "smtp.gmail.com", // hostname
secureConnection: true, // use SSL
port: 465, // port for secure SMTP
auth: {
user: "[email protected]",
pass: "['YourHotmailPassword']"
}
});
var mailOptions = {
from: "Node Emailer ✔ ", // sender address
to: req.body.email, // list of receivers
subject: req.body.subject+" ✔", // Subject line
//text: "Hello world ✔", // plaintext body
html: ""+req.body.description+"" // html body
}
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
res.send("Email could not sent due to error: "+error);
}else{
res.send("Email has been sent successfully");
}
}); */
});
// Starting server
var server = http.createServer(app).listen(port, function() {
console.log("Listening on " + port);
});
Step-5: Run
$ sudo npm install
on your terminal this will read all dependencies from package.json file and install them in your directory.
Step-6: Now finally open the terminal run command
$ node app.js
now you’ll see output like
Listening on 5000
this shows every thing is fine.
Finally time to check working url
http://your-hoostname:5000 hit on your browser if you are on localhost http://localhost:5000
You can see working demo by click on demo button and also download source code by download button.
Cheers Keep Coding 🙂