From 14041a319cace03cfc23c0a919ed81fb141f88ce Mon Sep 17 00:00:00 2001 From: Gavin Mogan Date: Fri, 25 Apr 2025 21:43:39 -0700 Subject: Refactor to have email service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Move hbsInstance back to app * Add email and hbs to req so typescript 🎉🎉🎉 * Init Email and config once --- src/lib/email.ts | 263 +++++++++++++++++++++++++++---------------------------- 1 file changed, 131 insertions(+), 132 deletions(-) (limited to 'src/lib/email.ts') diff --git a/src/lib/email.ts b/src/lib/email.ts index 57f69f5..7c7c2dd 100644 --- a/src/lib/email.ts +++ b/src/lib/email.ts @@ -1,11 +1,10 @@ import sgMail from "@sendgrid/mail"; import sgHelpers from "@sendgrid/helpers"; - +import { ExpressHandlebars } from "express-handlebars"; import nodemailer, { Transporter } from "nodemailer"; -import { getConfig } from "./config.js"; +import { GathioConfig, getConfig } from "./config.js"; import SMTPTransport from "nodemailer/lib/smtp-transport/index.js"; import { exitWithError } from "./process.js"; -import { HandlebarsSingleton } from "./handlebars.js"; const config = getConfig(); @@ -22,58 +21,65 @@ type EmailTemplateName = | "subscribed" | "unattendEvent"; -export const initEmailService = async (): Promise => { - if (process.env.CYPRESS || process.env.CI) { - console.log( - "Running in Cypress or CI, not initializing email service.", - ); - return false; - } - switch (config.general.mail_service) { - case "sendgrid": - if (!config.sendgrid?.api_key) { - return exitWithError( - "Sendgrid is configured as the email service, but no API key is provided. Please provide an API key in the config file.", - ); - } - sgMail.setApiKey(config.sendgrid.api_key); - console.log("Sendgrid is ready to send emails."); - return true; - case "nodemailer": { - let nodemailerTransporter: Transporter | undefined = undefined; - if (config.nodemailer?.smtp_url) { - nodemailerTransporter = nodemailer.createTransport( - config.nodemailer?.smtp_url, - ); - } else { - if ( - !config.nodemailer?.smtp_server || - !config.nodemailer?.smtp_port - ) { +export class EmailService { + nodemailerTransporter: Transporter | undefined = undefined; + sgMail: typeof sgMail | undefined = undefined; + hbs: ExpressHandlebars + + public constructor(config: GathioConfig, hbs: ExpressHandlebars) { + this.hbs = hbs; + switch (config.general.mail_service) { + case "sendgrid": { + if (!config.sendgrid?.api_key) { return exitWithError( - "Nodemailer is configured as the email service, but not all required fields are provided. Please provide all required fields in the config file.", + "Sendgrid is configured as the email service, but no API key is provided. Please provide an API key in the config file.", ); } - const nodemailerConfig = { - host: config.nodemailer?.smtp_server, - port: Number(config.nodemailer?.smtp_port) || 587, - tls: { - // do not fail on invalid certs - rejectUnauthorized: false, - }, - } as SMTPTransport.Options; + this.sgMail = sgMail; + this.sgMail.setApiKey(config.sendgrid.api_key); + console.log("Sendgrid is ready to send emails."); + break; + } + case "nodemailer": { + if (config.nodemailer?.smtp_url) { + this.nodemailerTransporter = nodemailer.createTransport( + config.nodemailer?.smtp_url, + ); + } else { + if ( + !config.nodemailer?.smtp_server || + !config.nodemailer?.smtp_port + ) { + return exitWithError( + "Nodemailer is configured as the email service, but not all required fields are provided. Please provide all required fields in the config file.", + ); + } + const nodemailerConfig = { + host: config.nodemailer?.smtp_server, + port: Number(config.nodemailer?.smtp_port) || 587, + tls: { + // do not fail on invalid certs + rejectUnauthorized: false, + }, + } as SMTPTransport.Options; - if (config.nodemailer?.smtp_username) { - nodemailerConfig.auth = { - user: config.nodemailer?.smtp_username, - pass: config.nodemailer?.smtp_password, - }; + if (config.nodemailer?.smtp_username) { + nodemailerConfig.auth = { + user: config.nodemailer?.smtp_username, + pass: config.nodemailer?.smtp_password, + }; + } + this.nodemailerTransporter = + nodemailer.createTransport(nodemailerConfig); } - nodemailerTransporter = - nodemailer.createTransport(nodemailerConfig); } - const nodemailerVerified = await nodemailerTransporter.verify(); + } + } + + public async verify(): Promise { + if (this.nodemailerTransporter) { + const nodemailerVerified = await this.nodemailerTransporter.verify(); if (nodemailerVerified) { console.log("Nodemailer is ready to send emails."); return true; @@ -83,30 +89,29 @@ export const initEmailService = async (): Promise => { ); } } - case "none": - default: - console.warn( - "You have not configured this Gathio instance to send emails! This means that event creators will not receive emails when their events are created, which means they may end up locked out of editing events. Consider setting up an email service.", - ); - return false; + return true; } -}; -export const sendEmail = async ( - to: string | string[], - bcc: string | string[] | undefined, - subject: string, - text: string, - html?: string, -): Promise => { - switch (config.general.mail_service) { - case "sendgrid": + public async sendEmail({ + to, + bcc, + subject, + text, + html, + }: { + to: string | string[]; + bcc?: string | string[]; + subject: string; + text: string; + html?: string; + }): Promise { + if (this.sgMail) { try { - await sgMail.send({ + await this.sgMail.send({ to, bcc, from: config.general.email, - subject: `${config.general.site_name}: ${subject}`, + subject, text, html, }); @@ -119,34 +124,13 @@ export const sendEmail = async ( } return false; } - case "nodemailer": + } else if (this.nodemailerTransporter) { try { - let nodemailerTransporter: Transporter | undefined = undefined; - if (config.nodemailer?.smtp_url) { - nodemailerTransporter = nodemailer.createTransport( - config.nodemailer?.smtp_url, - ); - } else { - const nodemailerConfig = { - host: config.nodemailer?.smtp_server, - port: Number(config.nodemailer?.smtp_port) || 587, - } as SMTPTransport.Options; - - if (config.nodemailer?.smtp_username) { - nodemailerConfig.auth = { - user: config.nodemailer?.smtp_username, - pass: config.nodemailer?.smtp_password, - }; - } - - nodemailerTransporter = - nodemailer.createTransport(nodemailerConfig); - } - await nodemailerTransporter.sendMail({ + await this.nodemailerTransporter.sendMail({ from: config.general.email, to, bcc, - subject: `${config.general.site_name}: ${subject}`, + subject, text, html, }); @@ -155,48 +139,63 @@ export const sendEmail = async ( console.error(e); return false; } - default: - return false; + } else { + // no mailer, so noop + return true; + } } -}; -export const sendEmailFromTemplate = async ( - to: string | string[], - bcc: string | string[] | undefined, - subject: string, - templateName: EmailTemplateName, - templateData: object, -): Promise => { - const [html, text] = await Promise.all([ - HandlebarsSingleton.instance.renderView( - `./views/emails/${templateName}/${templateName}Html.handlebars`, - { - domain: config.general.domain, - contactEmail: config.general.email, - siteName: config.general.site_name, - mailService: config.general.mail_service, - siteLogo: config.general.email_logo_url, - isFederated: config.general.is_federated || true, - cache: true, - layout: "email.handlebars", - ...templateData, - } - ), - HandlebarsSingleton.instance.renderView( - `./views/emails/${templateName}/${templateName}Text.handlebars`, - { - domain: config.general.domain, - contactEmail: config.general.email, - siteName: config.general.site_name, - mailService: config.general.mail_service, - siteLogo: config.general.email_logo_url, - isFederated: config.general.is_federated || true, - cache: true, - layout: "email.handlebars", - ...templateData, - } - ), - ]); + public async sendEmailFromTemplate({ + to, + bcc = "", + subject, + templateName, + templateData = {} + }: { + to: string | string[]; + bcc?: string | string[] | undefined; + subject: string; + templateName: EmailTemplateName; + templateData?: object; + }, + ): Promise { + const [html, text] = await Promise.all([ + this.hbs.renderView( + `./views/emails/${templateName}/${templateName}Html.handlebars`, + { + domain: config.general.domain, + contactEmail: config.general.email, + siteName: config.general.site_name, + mailService: config.general.mail_service, + siteLogo: config.general.email_logo_url, + isFederated: config.general.is_federated || true, + cache: true, + layout: "email.handlebars", + ...templateData, + } + ), + this.hbs.renderView( + `./views/emails/${templateName}/${templateName}Text.handlebars`, + { + domain: config.general.domain, + contactEmail: config.general.email, + siteName: config.general.site_name, + mailService: config.general.mail_service, + siteLogo: config.general.email_logo_url, + isFederated: config.general.is_federated || true, + cache: true, + layout: "email.handlebars", + ...templateData, + } + ), + ]); - return await sendEmail(to, bcc, subject, text, html); -}; + return this.sendEmail({ + to, + bcc, + subject: `${config.general.site_name}: ${subject}`, + text, + html + }); + } +} -- cgit v1.2.3