(bug)(subscription) promisify mailer – otherwise when sending subscriptions, emails are marked as sent even if there's been an error

in this case: "454 Throttling failure: Maximum sending rate exceeded."
This commit is contained in:
Andrea Vos 2025-02-02 15:57:38 +01:00
parent 19b3bf45ea
commit 79b5bf1c2d

View File

@ -23,30 +23,32 @@ if (!transport && process.env.NODE_ENV === 'development') {
} }
const transporter = nodemailer.createTransport(transport); const transporter = nodemailer.createTransport(transport);
const sendEmail = ( const sendEmail = async (
to: string, to: string,
subject: string, subject: string,
text: string | undefined = undefined, text: string | undefined = undefined,
html: string | undefined = undefined, html: string | undefined = undefined,
from: string | undefined = undefined, from: string | undefined = undefined,
): void => { ): Promise<void> => {
transporter.sendMail({ try {
const info = await transporter.sendMail({
to, to,
subject, subject,
text, text,
html, html,
from: from || process.env.MAILER_FROM, from: from || process.env.MAILER_FROM,
}, function (err, info) { });
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
const streamInfo = info as StreamTransport.SentMessageInfo; const streamInfo = info as StreamTransport.SentMessageInfo;
if (streamInfo.message) { if (streamInfo.message) {
(streamInfo.message as Readable).pipe(process.stdout); (streamInfo.message as Readable).pipe(process.stdout);
} }
} }
if (err) { } catch (err) {
Sentry.captureException(err); Sentry.captureException(err);
throw err;
} }
});
}; };
const findTranslation = (key: string) => { const findTranslation = (key: string) => {
@ -244,12 +246,12 @@ const applyTemplate = (
return templateText; return templateText;
}; };
export default (to: string, template: string | Template, params = {}): void => { export default async (to: string, template: string | Template, params = {}): Promise<void> => {
if (typeof template === 'string') { if (typeof template === 'string') {
template = templates[template]; template = templates[template];
} }
sendEmail( await sendEmail(
process.env.MAILER_OVERWRITE || to, process.env.MAILER_OVERWRITE || to,
applyTemplate(template, 'subject', params), applyTemplate(template, 'subject', params),
applyTemplate(template, 'text', params), applyTemplate(template, 'text', params),