require('dotenv').config();
//importing the module nodemailer
var nodemailer = require('nodemailer');
//importing smtp
var smtpTransport = require('nodemailer-smtp-transport');
var sendingEmail = {};
//method to send an email
sendingEmail = {
sendEmail: async function(recipient, subject, message, cb){
var transporter = nodemailer.createTransport(smtpTransport({
host: process.env.SMTP_SERVER,
port: process.env.SMTP_PORT,
secure: false,
auth: {
user: `${process.env.SMTP_USERNAME}`,
pass: `${process.env.SMTP_PASSWORD}`
},
tls: {
rejectUnauthorized: false
}
}));
var mailOptions = {
from: process.env.SMTP_USERNAME,
to: recipient,
subject: subject,
text: `From : ${recipient}
${message}`
};
await transporter.sendMail(mailOptions, (error, info) => {
if(error){
console.log(error);
}else{
console.log("Success: Email received from : ", recipient);
console.log(info.response);
}
});
}
}
module.exports = sendingEmail;