first commit
This commit is contained in:
34
src/utils/mailer.js
Normal file
34
src/utils/mailer.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const nodemailer = require('nodemailer');
|
||||
const ejs = require('ejs');
|
||||
const path = require('path');
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
service: 'gmail',
|
||||
auth: {
|
||||
user: process.env.EMAIL_USER,
|
||||
pass: process.env.EMAIL_PASS,
|
||||
},
|
||||
});
|
||||
|
||||
const sendEmail = async (to, subject, templateName, templateData) => {
|
||||
// Render EJS template
|
||||
const templatePath = path.join(__dirname, '../views/emails', `${templateName}.ejs`);
|
||||
const html = await ejs.renderFile(templatePath, templateData);
|
||||
|
||||
const mailOptions = {
|
||||
from: `"VC E-Commerce" <${process.env.EMAIL_USER}>`,
|
||||
to,
|
||||
subject,
|
||||
html,
|
||||
};
|
||||
|
||||
try {
|
||||
await transporter.sendMail(mailOptions);
|
||||
console.log('Email sent to', to);
|
||||
} catch (err) {
|
||||
console.error('Error sending email', err);
|
||||
throw new Error('Failed to send email');
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = sendEmail;
|
||||
Reference in New Issue
Block a user