I am trying to create a form that will send data to the Mongo DB first then will send that data through the email by Nodemailer. Here are the 2 functions:
contro
Create separate mail.js or anyname.js
var config = require('../config/config.js');
var nodemailer = require('nodemailer');
var smtpTransport = nodemailer.createTransport({
service :"gmail",
host: "smtp.gmail.com",
auth :
{
user: config.email,
pass: config.password
}
});
// setup email data with unicode symbols
var mailOptions = {
from: config.email,
to: 'user to send',
subject :'message',
text :' "Hi",\n You have successfully created an account"',
html: 'Welcome?' // html body
};
// sends mail
module.exports.sendMail = function()
{
// send mail with defined transport object
smtpTransport.sendMail(mailOptions, (error, info) => {
if (error)
{
return console.log(error);
}
console.log('Message sent: %s', info.messageId);});
}
now import this file in controller js file
var mailer = require('./mail.js');
and use it like below
mailer.sendMail()
you can pass values or params inside sendMail function and access them in mail.js file to create custom message or title or name any