Files
traxxx-web/src/web/emails.js
T

27 lines
834 B
JavaScript

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();
}