Added missing email API module.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user