From b2656345e8aa4cf2f6025c9644f972f6f709c906 Mon Sep 17 00:00:00 2001 From: DanielS Date: Thu, 25 Jun 2026 02:47:03 +0200 Subject: [PATCH] feat: Send email confirmation with PDF attachment upon successful order --- shop/lib/actions/orders.ts | 56 ++++++++++++++++++++++++++++++++++++++ shop/utils/mail.ts | 7 +++++ 2 files changed, 63 insertions(+) diff --git a/shop/lib/actions/orders.ts b/shop/lib/actions/orders.ts index b6a82c6..0187080 100644 --- a/shop/lib/actions/orders.ts +++ b/shop/lib/actions/orders.ts @@ -2,6 +2,7 @@ import { createClient } from '@/lib/supabase/server' import { revalidatePath } from 'next/cache' +import { sendMail } from '@/utils/mail' import { InvoicePDF } from '@/components/invoice-pdf' import { renderToBuffer } from '@react-pdf/renderer' import React from 'react' @@ -181,6 +182,61 @@ export async function submitOrder(params: { await supabase.from('orders').update({ pdf_url: publicUrlData.publicUrl }).eq('id', order.id) order.pdf_url = publicUrlData.publicUrl } + + // 7. E-Mail mit PDF-Anhang an den Besteller senden + if (user.email) { + try { + const formattedDate = new Date(order.created_at).toLocaleDateString('de-DE') + const formattedTotal = order.total_price.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }) + + await sendMail({ + to: user.email, + subject: `Bestellbestätigung ${orderNumber}`, + text: `Hallo,\n\nvielen Dank für Ihre Bestellung bei CASPOS Shop!\n\nBestelldetails:\n- Bestellnummer: ${orderNumber}\n- Datum: ${formattedDate}\n- Endkunde: ${customerSnapshot.company_name}\n- Gesamtsumme: ${formattedTotal}\n\nIm Anhang dieser E-Mail finden Sie Ihre Bestellbestätigung als PDF-Dokument.\n\nViele Grüße,\nIhr CASPOS Shop-Team`, + html: ` +
+

Vielen Dank für Ihre Bestellung!

+

Hallo,

+

wir freuen uns sehr über Ihre Bestellung bei CASPOS Shop. Ihre Bestellung wurde erfolgreich entgegengenommen und wird nun verarbeitet.

+
+

Bestelldetails

+ + + + + + + + + + + + + + + + + +
Bestellnummer:${orderNumber}
Datum:${formattedDate}
Endkunde:${customerSnapshot.company_name}
Gesamtsumme:${formattedTotal}
+
+

Im Anhang dieser E-Mail finden Sie Ihre Bestellbestätigung als PDF-Dokument.

+

Mit freundlichen Grüßen,
Ihr CASPOS Shop-Team

+
+

Diese E-Mail wurde automatisch generiert. Bitte antworten Sie nicht darauf.

+
+ `, + attachments: [ + { + filename: `Bestellbestaetigung_${orderNumber}.pdf`, + content: buffer, + contentType: 'application/pdf', + } + ] + }) + } catch (mailError) { + console.error('Failed to send order confirmation mail:', mailError) + } + } } catch (pdfError) { console.error('PDF Generation Error:', pdfError) } diff --git a/shop/utils/mail.ts b/shop/utils/mail.ts index 7c97d77..811ff06 100644 --- a/shop/utils/mail.ts +++ b/shop/utils/mail.ts @@ -14,11 +14,17 @@ export async function sendMail({ subject, text, html, + attachments, }: { to: string; subject: string; text: string; html?: string; + attachments?: { + filename: string; + content: Buffer | string; + contentType?: string; + }[]; }) { // Query custom SMTP settings from database const supabase = createAdminClient(); @@ -58,6 +64,7 @@ export async function sendMail({ subject, text, html, + attachments, }); return info;