I have implemented nodemailer
after the user registration, in the following way:
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
}
});
let mailOptions = {
from: process.env.EMAIL_USERNAME,
to: user.email,
subject: 'Verify your account',
text: 'Click here for verify your account'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
});
I don't like much this code because if I need to send an email in another module
, I need to rewrite all the stuff above.
Since I'm new to NodeJS
, I would like to know if I can remove this code redundancy make something like a utility
or maybe an helper
class. The goal is import the wrapper
class and call a simple function to send the email.
Which is the best way to handle that?