35 lines
896 B
JavaScript
35 lines
896 B
JavaScript
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;
|