From 62227c56527c6f232a90ad047f179766ec0bf513 Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Thu, 12 Nov 2026 21:34:22 +0100 Subject: [PATCH] Added missing email API module. --- src/web/emails.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/web/emails.js diff --git a/src/web/emails.js b/src/web/emails.js new file mode 100644 index 0000000..f03d226 --- /dev/null +++ b/src/web/emails.js @@ -0,0 +1,26 @@ +import { listEmailTemplates } from '../email-templates.js'; +import { sendTestEmail } from '../email.js'; +import { HttpError } from '../errors.js'; + +const emailPattern = /^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/; + +export async function sendTestEmailApi(req, res) { + // same access as the admin preview page + if (!req.user || req.user.role === 'user') { + throw new HttpError('You are not allowed to send test e-mails', 404); + } + + if (!listEmailTemplates().includes(req.params.template)) { + throw new HttpError(`E-mail template '${req.params.template}' does not exist`, 404); + } + + const to = typeof req.body?.to === 'string' ? req.body.to.trim() : null; + + if (!to || !emailPattern.test(to)) { + throw new HttpError('Please provide a valid e-mail address', 400); + } + + await sendTestEmail(req.params.template, to); + + res.status(204).send(); +}