This code snippet is used to send email via google's emailing service Gmail using node.js.
It also uses nodemailer
npm package of node.js for sending the mail. Before
proceeding further you need to allow access to non secure apps in your gmail account.
That is all, we are all set to send email using nodemailer and the snippet is given below :
//file-name : sending-mail.js
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'your-address@gmail.com', //email ID
pass: 'your-password-here' //Password
}
});
function sendMail(email , otp){
var details = {
from: 'your-address@gmail.com', // sender address same as above
to: email, // Receiver's email id
subject: 'Your demo OTP is ', // Subject of the mail.
html: otp // Sending OTP
};
transporter.sendMail(details, function (error, data) {
if(error)
console.log(error)
else
console.log(data);
});
}
var email = "info@nodejsera.com";
var otp = "123456";
sendMail(email,otp);
>node sending-mail.js