For sending emails using Nodejs we will be using The Nodemailer module.
Nodemailer is a module that provides sending emails from the NodeJs application.
 
Here are some features of the Nodemailer module:
 
1) Zero Dependencies.
2) Support email with attachments.
3) Support multiple services i.e Gmail, AOL, Hotmail, Yahoo, Godaddy etc.
4) Security.
 
Installation :
npm install nodemailer –save
 
Sample Code:
var  nodemailer = require('nodemailer');
//Using Gmail smtp transport
var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'gmail.user@gmail.com',
        pass: 'yourpass'
    }
});
// setup email data with unicode symbols
var mailOptions = {
    from: '"Ankit Chettri" <ankit.chettri@gmail.com>', // sender address
    to: 'test_example@evontech.com, test_example@evontech.com', // list of receivers
    subject: 'Hello', // Subject line
    text: 'Hello world', // plain text body
    html: '<b>Hello world </b>' // html body OR html file path as per requirement
};
// Code to send mail
transporter.sendMail(mailOptions, function(error, response) {
    if (error) {
        console.log(error);
    } else {
        console.log("Message sent: " + response.message);
    }
    transporter.close();
});
                       
                    
0 Comment(s)