Removed sendgrid, use emailService for all emails. Updated env vars, updated scripts
This commit is contained in:
1 parent
1c3c7e115a
commit
12e23cb156
10 files changed
+83
-996
No files matched your search
@@ -4,8 +4,20 @@ const nodemailer = require("nodemailer");
|
||||
const { compile } = require("handlebars");
|
||||
const { mjml2html } = require("mjml");
|
||||
|
||||
/**
|
||||
* Represents an email service that can load templates, build, and send emails.
|
||||
*/
|
||||
class EmailService {
|
||||
/**
|
||||
* Constructs an instance of the EmailService, initializing template loaders and the email transporter.
|
||||
*/
|
||||
constructor() {
|
||||
/**
|
||||
* Loads an email template from the filesystem.
|
||||
*
|
||||
* @param {string} templateName - The name of the template to load.
|
||||
* @returns {Function} A compiled template function that can be used to generate HTML email content.
|
||||
*/
|
||||
this.loadTemplate = (templateName) => {
|
||||
const templatePath = path.join(
|
||||
__dirname,
|
||||
@@ -15,7 +27,11 @@ class EmailService {
|
||||
return compile(templateContent);
|
||||
};
|
||||
|
||||
// TODO Load less used templates in their respective functions
|
||||
/**
|
||||
* A lookup object to access preloaded email templates.
|
||||
* @type {Object.<string, Function>}
|
||||
* TODO Load less used templates in their respective functions
|
||||
*/
|
||||
this.templateLookup = {
|
||||
welcomeEmailTemplate: this.loadTemplate("welcomeEmail"),
|
||||
employeeActivationTemplate: this.loadTemplate("employeeActivation"),
|
||||
@@ -25,17 +41,30 @@ class EmailService {
|
||||
passwordResetTemplate: this.loadTemplate("passwordReset"),
|
||||
};
|
||||
|
||||
/**
|
||||
* The email transporter used to send emails.
|
||||
* @type {Object}
|
||||
*/
|
||||
this.transporter = nodemailer.createTransport({
|
||||
host: process.env.EMAIL_SERVICE_HOST,
|
||||
port: process.env.EMAIL_SERVICE_PORT,
|
||||
host: process.env.SYSTEM_EMAIL_HOST,
|
||||
port: process.env.SYSTEM_EMAIL_PORT,
|
||||
secure: true, // Use `true` for port 465, `false` for all other ports
|
||||
auth: {
|
||||
user: process.env.EMAIL_SERVICE_USERNAME,
|
||||
pass: process.env.EMAIL_SERVICE_PASSWORD,
|
||||
user: process.env.SYSTEM_EMAIL_ADDRESS,
|
||||
pass: process.env.SYSTEM_EMAIL_PASSWORD,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously builds and sends an email using a specified template and context.
|
||||
*
|
||||
* @param {string} template - The name of the template to use for the email body.
|
||||
* @param {Object} context - The data context to render the template with.
|
||||
* @param {string} to - The recipient's email address.
|
||||
* @param {string} subject - The subject of the email.
|
||||
* @returns {Promise<string>} A promise that resolves to the messageId of the sent email.
|
||||
*/
|
||||
buildAndSendEmail = async (template, context, to, subject) => {
|
||||
const buildHtml = (template, context) => {
|
||||
const mjml = this.templateLookup[template](context);
|
||||
|
||||
Reference in new issue
Block a user