From a1317003434fb2a3444b2d59ae8b60da6aa922a9 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Wed, 12 Aug 2026 21:48:16 +0200 Subject: [PATCH] feat: fix mailer FROM header --- server/utils/handleStripeWebhook.ts | 6 ++--- server/utils/mailer.ts | 37 ++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/server/utils/handleStripeWebhook.ts b/server/utils/handleStripeWebhook.ts index 567468c..1911db6 100644 --- a/server/utils/handleStripeWebhook.ts +++ b/server/utils/handleStripeWebhook.ts @@ -1,13 +1,13 @@ import { useMailer } from './mailer'; import { usePapr } from './papr'; import { assignDiscordMemberSupporterRole, assignDiscordMemberTesterRole, removeDiscordMemberSupporterRole, removeDiscordMemberTesterRole, useDiscord } from './discord'; -import type { Transporter } from 'nodemailer'; import type { PaprMatchKeysAndValues } from 'papr'; import type { Stripe } from 'stripe'; import type { H3Event } from 'h3'; +import type { Mailer } from './mailer'; import type { PnidDocument } from './papr'; -async function sendEmailToCustomer(mailer: Transporter, customer: Stripe.Customer, ops: { pid: number; title: string; body: string }): Promise { +async function sendEmailToCustomer(mailer: Mailer, customer: Stripe.Customer, ops: { pid: number; title: string; body: string }): Promise { try { if (!customer.email) { throw new Error('Customer does not have an email'); @@ -22,7 +22,7 @@ async function sendEmailToCustomer(mailer: Transporter, customer: Stripe.Custome } } -async function sendToNotificationEmails(mailer: Transporter, notificationEmails: string[], ops: { title: string; body: string }): Promise { +async function sendToNotificationEmails(mailer: Mailer, notificationEmails: string[], ops: { title: string; body: string }): Promise { for (const email of notificationEmails) { // * Send notification emails for new sub try { diff --git a/server/utils/mailer.ts b/server/utils/mailer.ts index 399eed2..52f3f33 100644 --- a/server/utils/mailer.ts +++ b/server/utils/mailer.ts @@ -1,18 +1,33 @@ import { createTransport } from 'nodemailer'; +import type SMTPTransport from 'nodemailer/lib/smtp-transport'; +import type Mail from 'nodemailer/lib/mailer'; import type { Transporter } from 'nodemailer'; import type { H3Event } from 'h3'; -let transport: Transporter | null = null; +let mailer: Mailer | null = null; -export function useMailer(event: H3Event): Transporter | null { - if (!transport) { +export type Mailer = { + transport: Transporter; + sendMail(ops: Mail.Options & Partial): Promise; +}; + +function buildMailer(transport: Transporter, from: Mail.Address): Mailer { + return { + transport, + sendMail(ops) { + return transport.sendMail({ + from, + ...ops + }); + } + }; +} + +export function useMailer(event: H3Event): Mailer | null { + if (!mailer) { const config = useRuntimeConfig(event); if (config.smtpHost && config.smtpPort && config.smtpFromEmail) { - transport = createTransport({ - from: { - address: config.smtpFromEmail, - name: config.smtpFromName ? config.smtpFromName : undefined - }, + const transport = createTransport({ host: config.smtpHost, port: config.smtpPort, secure: config.smtpSecure, @@ -21,8 +36,12 @@ export function useMailer(event: H3Event): Transporter | null { pass: config.smtpPassword ? config.smtpPassword : undefined } }); + mailer = buildMailer(transport, { + address: config.smtpFromEmail, + name: config.smtpFromName ? config.smtpFromName : undefined + }); } } - return transport; + return mailer; }