Send Email with NodeJS

2020, Jun 17

Sending email in Nodejs requires a module named Nodemailer.
Nodemailer is a module for Node.js applications to allow easy as cake email sending

Requirements

Node.js v6.0.0 or newer. That’s it. If you are able to run Node.js version 6 or newer, then you can use Nodemailer. There are no platform or resource specific requirements

Installation

The Nodemailer module can be downloaded and installed using npm

npm install nodemailer

After you have downloaded the Nodemailer module, you can include the module in any application:

var nodemailer = require('nodemailer');

Sending an Email

Now you are ready to send emails from your server.

  1. Create a Nodemailer transporter using either SMTP or some other transport mechanism

    var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
            user: 'youremail@gmail.com',
            pass: 'yourpassword'
        }
    });

    If service: gmail, you need to allow Less Secure App in your gmail account and add

         tls:{
            rejectUnauthorized: false
        }

    to the transporter options

  2. Set up message options (who sends what to whom)

    var mailOptions = {
        from: 'youremail@gmail.com',
        to: 'myfriend@yahoo.com',
        subject: 'Sending Email using Node.js',
        text: 'That was easy!'
    };

    Multiple Receivers

    To send an email to more than one receiver, add them to the "to" property of the mailOptions object, separated by commas:

    var mailOptions = {
        from: 'youremail@gmail.com',
        to: 'myfriend@yahoo.com, myotherfriend@yahoo.com',
        subject: 'Sending Email using Node.js',
        text: 'That was easy!'
    }

    Send HTML

    To send HTML formatted text in your email, use the "html" property instead of the "text" property:

    var mailOptions = {
        from: 'youremail@gmail.com',
        to: 'myfriend@yahoo.com',
        subject: 'Sending Email using Node.js',
        html: '<h1>Welcome</h1><p>That was easy!</p>'
    }
  3. Deliver the message object using the sendMail() method of your previously created transporter

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

    In Conclusion,

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  },
    tls:{
        rejectUnauthorized: false
    }
});

var mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend@yahoo.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!',
  html: '<h1>Welcome</h1><p>That was easy!</p>'
};

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

Here is an example in my github making a complete project with it;