Implement a mailing solution in Express nodejs
Implement a mailing solution in Express nodejs
I'm creating an API that handles account verification via email, password recovery and possibly other things.
I want to create one place to send emails across my entire API.
i'm using nodeMailer.
My current setup is i'm a calling a method on the user model that send EmailVerification email.
I want to create a template outside the User model that could send either password recovery, email verification or other things.. Depending on the params that i pass to the function.
My user model:
userSchema.methods.generateEmailVerificationToken = function()
const token = jwt.sign(_id: this._id, role: this.role,
config.get('jwtPrivateKey'));
return token;
;
userSchema.methods.generateAuthToken = function()
const token = jwt.sign(_id: this._id, role: this.role,
config.get('jwtPrivateKey'));
return token;
;
userSchema.methods.sendEmailVerification = function (user)
sendMail(user);
;
and this is my sendMail function:
const nodemailer = require('nodemailer');
module.exports = function sendMail (user)
const transporter = nodemailer.createTransport(
service: 'gmail',
auth:
user: 'mymail@gmail.com',
pass: 'mypass'
);
const mailOptions =
from: 'mymail@gmail.com',
to: user.email,
subject: 'Email verification.',
html: `Please click this <a href="www.example.com/email_verif/$user.emailVerificationToken">link</a> to verify your email`
;
transporter.sendMail(mailOptions, function(error, info)
if (error)
console.log(error);
else
console.log('Email sent: ' + info.response);
);
Thank you
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You'll need to show us what you have so far and what errors or issues you are seeing. We can't help otherwise!
– jakerella
Aug 27 at 14:14