LibreChat/api/utils/sendEmail.js
Dan Orlando 30a49ae611
Feat email password reset (#730)
* change name of auth.service to AuthService

* Add emailEnabled to config api

* Setup email

* update nodemailer version

* add translations

* update .env.example

* clean up console.log's)

* refactor RequestPasswordReset component

* chore: rebuild package-lock.json

---------

Co-authored-by: Daniel Avila <messagedaniel@protonmail.com>
2023-07-31 22:37:46 -04:00

43 lines
1 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 {
const transporter = nodemailer.createTransport({
service: process.env.EMAIL_SERVICE,
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD,
},
});
const source = fs.readFileSync(path.join(__dirname, 'emails', template), 'utf8');
const compiledTemplate = handlebars.compile(source);
const options = () => {
return {
from: process.env.EMAIL_FROM,
to: email,
subject: subject,
html: compiledTemplate(payload),
};
};
// Send email
transporter.sendMail(options(), (error, info) => {
if (error) {
console.log(error);
return error;
} else {
console.log(info);
return info;
}
});
} catch (error) {
console.log(error);
return error;
}
};
module.exports = sendEmail;