Create Sent Mail API using NodeJS and Nodemailer NPM package.


Nodemailer is a module for Node.js applications to allow easy as cake email sending. The project got started back in 2010 when there was no sane option to send email messages, today it is the solution most Node.js users turn to by default.

Create a new Directory, we can run the following command: 

mkdir <directoryname>

then move into the newly created directory:

cd <directoryname>

then  run this command : 

npm init

and fill this information like that :

package name: (directoryname)
version: (1.0.0)
entry point: (index.js) server.js
test command:
git repository:
license: (ISC)
{"name": "curlapi",  "version": "1.0.0","description": "", "main": "server.js","scripts": { "test": "echo \"Error: no test specified\" && exit 1" },  "author": "",  "license": "ISC"}Is this ok? (yes) yes then enter

After creating project install node package run command:

npm install --save express morgan body-parser method-override multer nodemailer

Setup Mail Id

  You will have to Allow less secure apps: ON then only that mail can be sent.


After creating the server.js file and add this code :

// server.js
// set up ========================
var express  = require('express');
var app      = express();                              
var morgan = require('morgan');          
var mongoose = require('mongoose');
var bodyParser = require('body-parser');  
var methodOverride = require('method-override');
var nodemailer = require('nodemailer');
var multer  =   require('multer');

app.use(function(req, res, next) { //allow cross origin requests
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
    res.header("Access-Control-Max-Age", "3600");
    res.header("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// configuration
app.use(express.static(__dirname + '/public'));                 // set the static files location /public/img will be /img for users
app.use('/public/uploads',express.static(__dirname + '/public/uploads'));
app.use(bodyParser.json({limit: '50mb'}));                          // log every request to the console
app.use(bodyParser.urlencoded({limit: '50mb','extended':'true'}));            // parse application/x-www-form-urlencoded                                // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json

var transport = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
      user: "",
      pass: "Mailidpassword"

/** API for sending mail */'/api/sentmail', function(req, res) {
   var msg = {
      html: "<b>Hello!</b><p>Mail sent working</p>.",
      createTextFromHtml: true,
      from: "<>",
      to: "<>",
      subject: "Nodemail Credentials"
    transport.sendMail(msg, function (err) {
      if (err) {
      return res.json({"successMessage":"Credentials has been sent to your Email."});


app.listen(process.env.PORT || 9000, function(){console.log("App listening on port 9000");});


Github Link:

See demo:

About the author
Code solution


Add comment To Login
Add comment