How to Send Email from Node.js

Channel: Linux
Abstract: [email protected]')return console.log(error)

This tutorial will show you to how to send email through node.js application via Gmail smtp server.

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 protected]');

var mailOptions = {
   from: "Sender Name <[email protected]>",
   to: "Recipient Name <[email protected]>",
   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.

Ref From: tecadmin

Related articles