How to Send Email from Node.js

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_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.

Thanks for Visit Here

Comments