From 79b5bf1c2dc2d0fb17db233fcf9d428e4e0ec540 Mon Sep 17 00:00:00 2001 From: Andrea Vos Date: Sun, 2 Feb 2025 15:57:38 +0100 Subject: [PATCH] =?UTF-8?q?(bug)(subscription)=20promisify=20mailer=20?= =?UTF-8?q?=E2=80=93=20otherwise=20when=20sending=20subscriptions,=20email?= =?UTF-8?q?s=20are=20marked=20as=20sent=20even=20if=20there's=20been=20an?= =?UTF-8?q?=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit in this case: "454 Throttling failure: Maximum sending rate exceeded." --- server/mailer.ts | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/server/mailer.ts b/server/mailer.ts index 2e150bb87..e3879023b 100644 --- a/server/mailer.ts +++ b/server/mailer.ts @@ -23,30 +23,32 @@ if (!transport && process.env.NODE_ENV === 'development') { } const transporter = nodemailer.createTransport(transport); -const sendEmail = ( +const sendEmail = async ( to: string, subject: string, text: string | undefined = undefined, html: string | undefined = undefined, from: string | undefined = undefined, -): void => { - transporter.sendMail({ - to, - subject, - text, - html, - from: from || process.env.MAILER_FROM, - }, function (err, info) { +): Promise => { + try { + const info = await transporter.sendMail({ + to, + subject, + text, + html, + from: from || process.env.MAILER_FROM, + }); + if (process.env.NODE_ENV === 'development') { const streamInfo = info as StreamTransport.SentMessageInfo; if (streamInfo.message) { (streamInfo.message as Readable).pipe(process.stdout); } } - if (err) { - Sentry.captureException(err); - } - }); + } catch (err) { + Sentry.captureException(err); + throw err; + } }; const findTranslation = (key: string) => { @@ -244,12 +246,12 @@ const applyTemplate = ( return templateText; }; -export default (to: string, template: string | Template, params = {}): void => { +export default async (to: string, template: string | Template, params = {}): Promise => { if (typeof template === 'string') { template = templates[template]; } - sendEmail( + await sendEmail( process.env.MAILER_OVERWRITE || to, applyTemplate(template, 'subject', params), applyTemplate(template, 'text', params),