Fix mail sending

This commit is contained in:
Clément Pons 2025-04-22 09:28:48 +02:00
parent 7beeed8a61
commit 735dfb6980
5 changed files with 50 additions and 52 deletions

View File

@ -5,6 +5,7 @@ import { usersDataTable, usersTable } from '~/db/schema';
import { schema } from '~/schemas/registration';
import { checkSession, logSession } from '~/server/utils/user';
import type { UserSession, UserSessionRequired } from '~/types/auth';
import sendMail from '~/server/tasks/mail';
interface SuccessHandler
{
@ -82,7 +83,7 @@ export default defineEventHandler(async (e): Promise<Return> => {
id: emailId, timestamp,
}
});
await runTask('mail', {
await sendMail({
payload: {
type: 'mail',
to: [body.data.email],

View File

@ -3,6 +3,7 @@ import { eq, or } from 'drizzle-orm';
import { z } from 'zod';
import useDatabase from '~/composables/useDatabase';
import { usersTable } from '~/db/schema';
import sendMail from '~/server/tasks/mail';
const schema = z.object({
profile: z.string(),
@ -32,7 +33,7 @@ export default defineEventHandler(async (e) => {
id, timestamp,
}
});
await runTask('mail', {
await sendMail({
payload: {
type: 'mail',
data: {

View File

@ -5,6 +5,7 @@ import { usersDataTable, usersTable } from '~/db/schema';
import { schema } from '~/schemas/registration';
import { checkSession, logSession } from '~/server/utils/user';
import type { UserSession, UserSessionRequired } from '~/types/auth';
import sendMail from '~/server/tasks/mail';
interface SuccessHandler
{
@ -73,7 +74,7 @@ export default defineEventHandler(async (e): Promise<Return> => {
logSession(e, await setUserSession(e, { user: { id: id.id, username: body.data.username, email: body.data.email, state: 0, signin: new Date(), permissions: [], lastTimestamp: new Date(), logCount: 1 } }) as UserSessionRequired);
await runTask('mail', {
await sendMail({
payload: {
type: 'mail',
to: [body.data.email],

View File

@ -2,6 +2,7 @@ import { hash } from "bun";
import { eq } from "drizzle-orm";
import useDatabase from "~/composables/useDatabase";
import { usersTable } from "~/db/schema";
import sendMail from '~/server/tasks/mail';
export default defineEventHandler(async (e) => {
const session = await getUserSession(e);
@ -56,7 +57,7 @@ export default defineEventHandler(async (e) => {
id: emailId, timestamp,
}
});
await runTask('mail', {
await sendMail({
payload: {
type: 'mail',
to: [data.email],

View File

@ -41,56 +41,50 @@ const transport = nodemailer.createTransport({
},
});
export default defineTask({
meta: {
name: 'mail',
description: 'Send email',
},
async run(e) {
try {
if(e.payload.type !== 'mail')
{
throw new Error(`Données inconnues`);
}
const payload = e.payload as MailPayload;
const template = templates[payload.template];
if(!template)
{
throw new Error(`Modèle de mail ${payload.template} inconnu`);
}
console.time('Generating HTML');
const mail: Mail.Options = {
from: 'd[any] - Ne pas répondre <no-reply@peaceultime.com>',
to: payload.to,
html: await render(template.component, payload.data),
subject: template.subject,
textEncoding: 'quoted-printable',
};
console.timeEnd('Generating HTML');
if(mail.html === '')
return { result: false, error: new Error("Invalid content") };
console.time('Sending Mail');
const status = await transport.sendMail(mail);
console.timeEnd('Sending Mail');
if(status.rejected.length > 0)
{
return { result: false, error: status.response, details: status.rejectedErrors };
}
return { result: true };
}
catch(e)
export default async function(e: TaskEvent) {
try {
if(e.payload.type !== 'mail')
{
return { result: false, error: e };
throw new Error(`Données inconnues`);
}
},
})
const payload = e.payload as MailPayload;
const template = templates[payload.template];
if(!template)
{
throw new Error(`Modèle de mail ${payload.template} inconnu`);
}
console.time('Generating HTML');
const mail: Mail.Options = {
from: 'd[any] - Ne pas répondre <no-reply@peaceultime.com>',
to: payload.to,
html: await render(template.component, payload.data),
subject: template.subject,
textEncoding: 'quoted-printable',
};
console.timeEnd('Generating HTML');
if(mail.html === '')
return { result: false, error: new Error("Invalid content") };
console.time('Sending Mail');
const status = await transport.sendMail(mail);
console.timeEnd('Sending Mail');
if(status.rejected.length > 0)
{
return { result: false, error: status.response, details: status.rejectedErrors };
}
return { result: true };
}
catch(e)
{
return { result: false, error: e };
}
}
async function render(component: any, data: Record<string, any>): Promise<string>
{