From 2f25937a94c8b0e7d68a78ca660a3cb7feb316ae Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Tue, 18 Mar 2025 23:57:01 +0100 Subject: [PATCH] feat: add ical --- src/queues/email.queue.ts | 5 ++++- src/routes/email.ts | 10 +++++++++- src/services/email.ts | 2 ++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/queues/email.queue.ts b/src/queues/email.queue.ts index d82970b..235d303 100644 --- a/src/queues/email.queue.ts +++ b/src/queues/email.queue.ts @@ -2,11 +2,13 @@ import { createTransport } from "nodemailer"; import { Queue, Worker, QueueEvents } from "bullmq"; import { config } from "../config/env"; import Redis from "ioredis"; +import { Attachment } from "nodemailer/lib/mailer"; interface EmailJob { to: string; subject: string; html: string; + attachments: Attachment[]; text: string; } @@ -40,12 +42,13 @@ const worker = new Worker( QUEUE_NAME, async (job) => { await transporter.sendMail({ - from: config.email.from, + from: { address: config.email.from, name: "Lauf für Kaya!" }, replyTo: config.email.replyTo, to: job.data.to, subject: job.data.subject, text: job.data.text, html: job.data.html, + attachments: job.data.attachments, }); }, { diff --git a/src/routes/email.ts b/src/routes/email.ts index cc190f5..d4fdaec 100644 --- a/src/routes/email.ts +++ b/src/routes/email.ts @@ -5,6 +5,7 @@ import { z } from 'zod' import { EmailService } from '../services/email' import { getEmailTemplate } from '../templates' import { Language } from '../types' +import { Attachment } from 'nodemailer/lib/mailer' const emailRouter = new Hono() const emailService = new EmailService() @@ -22,7 +23,7 @@ async function generateBarcodeDataURL(data) { emailRouter.post('/', bearerAuth({ token: process.env.AUTHKEY }), zValidator('json', sendEmailSchema), async (c) => { let { to, templateName, language, data } = c.req.valid('json') - + const attachments: Attachment[] = [] try { const template = getEmailTemplate(templateName, language as Language) if (templateName === "welcome") { @@ -32,6 +33,12 @@ emailRouter.post('/', bearerAuth({ token: process.env.AUTHKEY }), zValidator('js } else { return c.json({ success: false, error: "required params 'data.name', 'data.link', 'data.barcode_content' not provided" }, 406) } + const attachment: Attachment = { + filename: 'invite.ics', + content: `BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//ICS Generator//NONSGML ICS Generator//DE\nBEGIN:VEVENT\nUID:1742337822408-5ghrzyi@icsgenerator.local\nDTSTAMP:20250318T224342Z\nSUMMARY:Lauf für Kaya! 2025\nDTSTART:20250523T110000Z\nDTEND:20250523T160000Z\nDESCRIPTION:Der Lauf für Kaya! 2025 findet am 23.05.2025 auf dem Sportplatz des Gymnasium Herzogenaurach statt. Zur Anmeldung einfach zum Infozelt kommen.\nLOCATION:Sportplatz Gymnasium Herzogenaurach\nBEGIN:VALARM\nACTION:DISPLAY\nDESCRIPTION:Erinnerung: Lauf für Kaya! 2025\nTRIGGER:-PT1440M\nEND:VALARM\nEND:VEVENT\nEND:VCALENDAR`, + contentType: 'text/calendar; method=REQUEST', + } + attachments.push(attachment) } if (templateName === "password-reset") { if (data.token) { @@ -45,6 +52,7 @@ emailRouter.post('/', bearerAuth({ token: process.env.AUTHKEY }), zValidator('js data.event_name = process.env.EVENT_NAME await emailService.sendEmail({ to, + attachments, subject: template.subject(data), html: template.html(data), text: template.text(data) diff --git a/src/services/email.ts b/src/services/email.ts index 3353087..c7982b7 100644 --- a/src/services/email.ts +++ b/src/services/email.ts @@ -1,10 +1,12 @@ import { emailQueue } from '../queues/email.queue' import { config } from '../config/env' +import { Attachment } from 'nodemailer/lib/mailer' interface EmailOptions { to: string subject: string html: string + attachments: Attachment[] text: string }