mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
54 lines
1.2 KiB
JavaScript
54 lines
1.2 KiB
JavaScript
|
|
const nodemailer = require("nodemailer");
|
||
|
|
const handlebars = require("handlebars");
|
||
|
|
const fs = require("fs");
|
||
|
|
const path = require("path");
|
||
|
|
|
||
|
|
const sendEmail = async (email, subject, payload, template) => {
|
||
|
|
try {
|
||
|
|
// create reusable transporter object using the default SMTP transport
|
||
|
|
const transporter = nodemailer.createTransport({
|
||
|
|
host: process.env.EMAIL_HOST,
|
||
|
|
port: 465,
|
||
|
|
auth: {
|
||
|
|
user: process.env.EMAIL_USERNAME,
|
||
|
|
pass: process.env.EMAIL_PASSWORD,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const source = fs.readFileSync(path.join(__dirname, template), "utf8");
|
||
|
|
const compiledTemplate = handlebars.compile(source);
|
||
|
|
const options = () => {
|
||
|
|
return {
|
||
|
|
from: process.env.FROM_EMAIL,
|
||
|
|
to: email,
|
||
|
|
subject: subject,
|
||
|
|
html: compiledTemplate(payload),
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
// Send email
|
||
|
|
transporter.sendMail(options(), (error, info) => {
|
||
|
|
if (error) {
|
||
|
|
return error;
|
||
|
|
} else {
|
||
|
|
return res.status(200).json({
|
||
|
|
success: true,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
return error;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/*
|
||
|
|
Example:
|
||
|
|
sendEmail(
|
||
|
|
"youremail@gmail.com,
|
||
|
|
"Email subject",
|
||
|
|
{ name: "Eze" },
|
||
|
|
"./templates/layouts/main.handlebars"
|
||
|
|
);
|
||
|
|
*/
|
||
|
|
|
||
|
|
module.exports = sendEmail;
|