Hi I'm using nodemailer and I need to change the password if the user logged in with the personal email. I want to create a separate function that can sent emails for multiple purposes like password reset, order confirmations, etc.
I am using nodemailer-express-handlebars and want to sent a template located in views, and create the logic within the /api/reset-password endpoint.
const {sendEmail} = require('../../services/nodemailer.js');
const fs = require('fs');
const path = require('path');
exports.reset = (req, res, next) => {
const {userID, email} = req.body;
const emailTemplateSource = fs.readFileSync(path.join(__dirname, '../../views/password-refresh.handlebars'), "utf8")
res.send({userID, email});
sendEmail(email, {
subject: 'Password recovery',
text: 'Please use this page to reset your password. Your password will be securey stored in our database and will be available imediatelly.',
html: emailTemplateSource
});
}
Above is the current configuration of the controller.emailTemplateSource is the try where I attempt to locate the view and use the html view inside the email to change it.
Beside that the view does not load the CSS, if I press Reset button, the gmail send me to an external page..... This is a behaviour I need to change. Or find another solution...
Bellow is the nodemailer function where I try to implement the send functionality with parameters, in order to be reusable.
const nodemailer = require('nodemailer');
require('dotenv').config()
const hbs = require('nodemailer-express-handlebars');
exports.sendEmail = async (userEmail, options) => {
let transporter = nodemailer.createTransport({
host: "smtp-mail.outlook.com",
secureConnection: false,
port: 587,
tls: {
ciphers:'SSLv3'
},
auth: {
user: process.env.BUSINESS_EMAIL,
pass: process.env.EMAIL_PASSWORD
}
});
/* Configure the nodemailer for the handlebars views */
transporter.use('compile', hbs({
viewEngine: 'express-handlebars',
viewPath: './views/'
}));
let mailOptions = {
from: process.env.BUSINESS_EMAIL,
to: userEmail,
subject: options.subject,
text: options.text,
html: options.html
}
transporter.sendMail(mailOptions, (error, success) => {
if (error) return console.log(error);
else return console.log('Email send');
})
}
Anyone know what should I do? The template send me to another page
Thanks, Daniel