Hack The Sec - Leading Resource Of Linux Tutorial
How to Send Email from Node.js

How to Send Email from Node.js


Send Email from Node.js

Node.js is an open-source, cross-platform runtime environment for developing server-side Web applications. Although Node.js is not a JavaScript framework, many of its basic modules are written in JavaScript, and developers can write new modules in JavaScript. The runtime environment interprets JavaScript using Google's V8 JavaScript engine.

First you need to install nodemailer package in your application. Use following command to install this package.
$ npm install nodemailer
Now add following code in your application to send email. Make sure to update all required values in below code to send email successfully.
var nodemailer = require('nodemailer');

var mailTransport = nodemailer.createTransport('smtps://user%40gmail.com:email_password@smtp.gmail.com');

var mailOptions = {
   from: "Sender Name <sender@example.com>",
   to: "Recipient Name <recipient@example.com>",
   subject: "Hello World",
   text: "Test email with node.js"
   html: '<b>Test email with node.js</b>'
};


mailTransport.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);
});
If you are still facing any issue with sending email through Gmail stmp servers make sure you are using correct login details. For 2 factor authentication enabled account required to generate application specific password and set here. Also you allow less secure apps in your Gmail account.

H

About the author

I am a Linux Administrator and Security Expert. Through this site I share Linux tutorials, hardening guides and security news.

Comments