How to send email in node js using NodeMailer and Gmail

How to add Admob Pro in Ionic 1
August 8, 2018
TypeError: this.driver.matches Element is not a function at Transition Animation Engine
August 11, 2018

How to send email in node js using NodeMailer and Gmail

We can easily send email in node js using NodeMailer module. NodeMailer requires email transport service like gmail to send email.

First of all, install NodeMailer package to your application.

npm install --save nodemailer

Include this in your code.

var nodemailer = require('nodemailer');

Since we are using Gmail as transport service, we have to provide our email credentials so that gmail allow nodemailer to send email using given gmail account.

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

Now we can send email using sendMail method of transporter object.

 

var msg = {
    html: "Your html code"
    createTextFromHtml: true,
    from: 'sender@email.com',
    to:'receiver@email.com',
    subject: "Email subject here"
};

transporter.sendMail(msg, function (err, info) {
    if(err)
      console.log(err)
    else
      console.log(info);
});

 

Leave a Reply