Added e-mail test button.
This commit is contained in:
@@ -2,6 +2,7 @@ import config from 'config';
|
||||
import { Resend } from 'resend';
|
||||
|
||||
import { emailTemplates } from './email-templates.js';
|
||||
import { HttpError } from './errors.js';
|
||||
import initLogger from './logger.js';
|
||||
|
||||
const logger = initLogger();
|
||||
@@ -82,3 +83,33 @@ export async function sendEmails(items) {
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// sends a template the way the admin preview displays it, rendered with the template's own mock props
|
||||
export async function sendTestEmail(template, to) {
|
||||
if (!config.email.enabled) {
|
||||
throw new HttpError('E-mails are disabled', 503);
|
||||
}
|
||||
|
||||
if (!resend) {
|
||||
throw new HttpError('E-mails are enabled, but Resend is not initialized', 503);
|
||||
}
|
||||
|
||||
const { html, text } = await emailTemplates.render(`${template}.vue`);
|
||||
|
||||
const { data, error } = await resend.emails.send({
|
||||
from: config.email.from,
|
||||
to,
|
||||
subject: `Test: ${template}`,
|
||||
html,
|
||||
text,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
logger.error(`Failed to send '${template}' test e-mail to '${to}' (${error.statusCode} ${error.name}): ${error.message}`);
|
||||
throw new HttpError(`Failed to send test e-mail: ${error.message}`, 502);
|
||||
}
|
||||
|
||||
logger.info(`Sent '${template}' test e-mail to '${to}'`);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import { authRouter, setUserApi } from './auth.js';
|
||||
|
||||
import consentHandler from './consent.js';
|
||||
|
||||
import { sendTestEmailApi } from './emails.js';
|
||||
import { fetchEntitiesApi } from './entities.js';
|
||||
import errorHandler from './error.js';
|
||||
import { graphqlApi } from './graphql.js';
|
||||
@@ -143,6 +144,7 @@ export default async function initServer() {
|
||||
router.get('/api/tags', fetchTagsApi);
|
||||
|
||||
router.post('/api/caches', initCachesApi);
|
||||
router.post('/api/admin/emails/:template/test', sendTestEmailApi);
|
||||
|
||||
if (config.apiAccess.graphqlEnabled) {
|
||||
router.post('/graphql', graphqlApi);
|
||||
|
||||
Reference in New Issue
Block a user