summaryrefslogtreecommitdiff
path: root/src/lib/email.ts
diff options
context:
space:
mode:
authorRaphael Kabo <raphaelkabo@hey.com>2023-10-07 14:30:24 +0100
committerRaphael Kabo <raphaelkabo@hey.com>2023-10-07 15:38:47 +0100
commitb795d07ed7a1b705b72b171f8e8de267a720223b (patch)
treeb8ae3df8dbb89f839f29328e817f030dc22b89f8 /src/lib/email.ts
parent9341659fd7a791d77454dd33743e42d952dbd202 (diff)
refactor: event form and api routes
Diffstat (limited to 'src/lib/email.ts')
-rw-r--r--src/lib/email.ts151
1 files changed, 151 insertions, 0 deletions
diff --git a/src/lib/email.ts b/src/lib/email.ts
new file mode 100644
index 0000000..f1dc1ae
--- /dev/null
+++ b/src/lib/email.ts
@@ -0,0 +1,151 @@
+import { Request } from "express";
+import sgMail from "@sendgrid/mail";
+import nodemailer, { TransportOptions } from "nodemailer";
+import { getConfig } from "./config.js";
+import SMTPTransport from "nodemailer/lib/smtp-transport/index.js";
+import { exitWithError } from "./process.js";
+import { renderTemplate } from "./handlebars.js";
+const config = getConfig();
+
+type EmailTemplate =
+ | "addEventAttendee"
+ | "addEventComment"
+ | "createEvent"
+ | "createEventGroup"
+ | "deleteEvent"
+ | "editEvent"
+ | "eventGroupUpdated"
+ | "subscribed"
+ | "unattendEvent";
+
+export const initEmailService = async (): Promise<boolean> => {
+ 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":
+ if (
+ !config.nodemailer?.smtp_server ||
+ !config.nodemailer?.smtp_port ||
+ !config.nodemailer?.smtp_username ||
+ !config.nodemailer?.smtp_password
+ ) {
+ 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,
+ auth: {
+ user: config.nodemailer?.smtp_username,
+ pass: config.nodemailer?.smtp_password,
+ },
+ } as SMTPTransport.Options;
+ const nodemailerTransporter =
+ nodemailer.createTransport(nodemailerConfig);
+ const nodemailerVerified = await nodemailerTransporter.verify();
+ if (nodemailerVerified) {
+ console.log("Nodemailer is ready to send emails.");
+ return true;
+ } else {
+ return exitWithError(
+ "Error verifying Nodemailer transporter. Please check your Nodemailer configuration.",
+ );
+ }
+ 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;
+ }
+};
+
+export const sendEmail = async (
+ to: string,
+ subject: string,
+ text: string,
+ html?: string,
+): Promise<boolean> => {
+ switch (config.general.mail_service) {
+ case "sendgrid":
+ try {
+ await sgMail.send({
+ to,
+ from: config.general.email,
+ subject: `${config.general.site_name}: ${subject}`,
+ text,
+ html,
+ });
+ return true;
+ } catch (e: any) {
+ if (e.response) {
+ console.error(e.response.body);
+ } else {
+ console.error(e);
+ }
+ return false;
+ }
+ case "nodemailer":
+ try {
+ const nodemailerConfig = {
+ host: config.nodemailer?.smtp_server,
+ port: Number(config.nodemailer?.smtp_port) || 587,
+ auth: {
+ user: config.nodemailer?.smtp_username,
+ pass: config.nodemailer?.smtp_password,
+ },
+ } as SMTPTransport.Options;
+ const nodemailerTransporter =
+ nodemailer.createTransport(nodemailerConfig);
+ await nodemailerTransporter.sendMail({
+ from: config.general.email,
+ to,
+ subject,
+ text,
+ html,
+ });
+ return true;
+ } catch (e) {
+ console.error(e);
+ return false;
+ }
+ default:
+ return false;
+ }
+};
+
+export const sendEmailFromTemplate = async (
+ to: string,
+ subject: string,
+ template: EmailTemplate,
+ templateData: Record<string, unknown>,
+ req: Request,
+): Promise<boolean> => {
+ const html = await renderTemplate(req, `${template}/${template}Html`, {
+ siteName: config.general.site_name,
+ siteLogo: config.general.email_logo_url,
+ domain: config.general.domain,
+ cache: true,
+ layout: "email.handlebars",
+ ...templateData,
+ });
+ const text = await renderTemplate(
+ req,
+ `${template}/${template}Text`,
+ templateData,
+ );
+ return await sendEmail(to, subject, text, html);
+};