Nodejs RESTful API Email Verification

Date:

5 Oct 2019

  • my_modules/SimpleGmail.js

  • dependency: nodemailer

module.exports = class SimpleGmail{
    constructor(myAddress, myPassword, receiverAddress){
        var self = this;
        self.myAddress = myAddress;
        self.myPassword = myPassword;
        self.receiverAddress = receiverAddress;
        self.nodemailer = require('nodemailer');
        self.transporter = self.nodemailer.createTransport({
            service: 'gmail',
            auth: {user:myAddress,pass:myPassword}
        });

    }

    send(subject, html){
        var self = this;
        var mailOptions = {
            from: self.myAddress, to: self.receiverAddress,
            subject: subject, html: html
        }
        self.transporter.sendMail(mailOptions,(err,info)=>{
            if(err){
                console.log("Error: ",err);
                console.log("Make sure this is ON: https://myaccount.google.com/lesssecureapps");
            }
        });
    }
}