Add mail template, mail HTML generation and a few UI fixes

This commit is contained in:
2024-11-25 17:37:43 +01:00
parent d71e8b7910
commit 4df9297d47
12 changed files with 171 additions and 20 deletions

View File

@@ -1,9 +1,34 @@
import nodemailer from 'nodemailer';
import { createSSRApp, h } from 'vue';
import { renderToString } from 'vue/server-renderer';
import base from '../components/mail/base.vue';
import registration from '../components/mail/registration.vue';
import revalidation from '../components/mail/revalidation.vue';
const config = useRuntimeConfig();
const [domain, selector, dkim] = config.mail.dkim.split(":");
export const templates: Record<string, { component: any, subject: string }> = {
"registration": { component: registration, subject: 'Bienvenue sur d[any] 😎' },
"revalidate-mail": { component: revalidation, subject: 'd[any]: Valider votre email' },
};
import 'nitropack/types';
import type Registration from '../components/mail/registration.vue';
declare module 'nitropack/types'
{
interface TaskPayload
{
type: 'mail'
to: string[]
template: string
data: Record<string, any>
}
}
const transport = nodemailer.createTransport({
//@ts-ignore
pool: true,
host: config.mail.host,
port: config.mail.port,
@@ -27,13 +52,35 @@ export default defineTask({
},
async run(e) {
try {
const payload: { to: string[], message: string, subject: string } = e.payload;
const status = await transport.sendMail({
from: 'Message automatique d[any] <no-reply@peaceultime.com>',
if(e.payload.type !== 'mail')
{
throw new Error(`Données inconnues`);
}
const payload = e.payload;
const template = templates[payload.template];
if(!template)
{
throw new Error(`Modèle de mail ${payload.template} inconnu`);
}
const mail = {
from: 'd[any] - Ne pas répondre <no-reply@peaceultime.com>',
to: payload.to,
text: payload.message,
subject: payload.subject,
});
html: await render(template.component, payload.data),
subject: template.subject,
attachments: [{
filename: 'logo.svg',
path: '../../public/logo.dark.svg',
cid: 'logo.obsidian.peaceultime.com',
}]
};
if(mail.html === '')
return { result: false, error: new Error("Invalid content") };
const status = await transport.sendMail(mail);
if(status.rejected.length > 0)
{
@@ -47,4 +94,15 @@ export default defineTask({
return { result: false, error: e };
}
},
})
})
async function render(component: any, data: Record<string, any>): Promise<string>
{
const app = createSSRApp({
render(){
return h(base, null, { default: () => h(component, data, []) });
}
});
return await renderToString(app);
}