Send Email using Node.js

Aditya Rawas - May 8 '21 - - Dev Community

Want to send email using node.js ??,
Its simple here are some steps that will allow you to send email using node.js using nodemailer.

STEP 1 - Install nodemailer

Use following command to install nodemailer

npm install nodemailer
Enter fullscreen mode Exit fullscreen mode

STEP 2 - Create Transporter

import nodemailer and create transporter

var nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    service: 'gmail' // here I am using gmail service
    auth:{
       user: 'emailid@gmail.com,
       passoword: 'your gmail app passsword'

    }
});
Enter fullscreen mode Exit fullscreen mode

Remember the password you are going to use is not the password for email rather you will have to generate application password provided by gmail. Click here to see how to generate application password

STEP 3 - Create Options

let mailOptions = {
  to: 'myfriend@yahoo.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
};
Enter fullscreen mode Exit fullscreen mode

STEP 4 - Send Email

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});
Enter fullscreen mode Exit fullscreen mode

IF YOU LIKED THIS ARTICLE MAKE SURE TO FOLLOW ME ON DEV.TO AND ON INSTAGRAM

Aditya Rawas Coffee

. . . . . . . . . . . . . . . . . . . . .
Terabox Video Player