import nodemailer from 'nodemailer'; import path from 'path'; import { readFile } from 'fs/promises'; let transporter: nodemailer.Transporter | null = null; function getTransporter() { if (transporter) return transporter; const smtpHost = process.env.SMTP_HOST; const smtpPort = parseInt(process.env.SMTP_PORT || '587'); const smtpUser = process.env.SMTP_USER; const smtpPass = process.env.SMTP_PASS; const smtpFrom = process.env.SMTP_FROM || 'noreply@savethemoment.photos'; if (!smtpHost || !smtpUser || !smtpPass) { console.warn('⚠️ SMTP credentials not configured. Email sending disabled.'); throw new Error('SMTP not configured'); } transporter = nodemailer.createTransport({ host: smtpHost, port: smtpPort, secure: smtpPort === 465, auth: { user: smtpUser, pass: smtpPass, }, }); console.log('✅ SMTP transporter initialized'); return transporter; } interface SendEmailOptions { to: string; subject: string; text: string; html: string; attachments?: { filename: string; content?: Buffer; path?: string; }[]; } export async function sendEmail(options: SendEmailOptions) { try { const transport = getTransporter(); const from = process.env.SMTP_FROM || 'SaveTheMoment '; const info = await transport.sendMail({ from, to: options.to, subject: options.subject, text: options.text, html: options.html, attachments: options.attachments, }); console.log('✅ Email sent:', info.messageId); return { success: true, messageId: info.messageId }; } catch (error: any) { console.error('❌ Email send error:', error); throw error; } } export async function sendContractEmail( booking: any, contractPdfPath: string ) { const signToken = Buffer.from(`${booking.id}-${Date.now()}`).toString('base64url'); const signUrl = `${process.env.NEXTAUTH_URL}/contract/sign/${signToken}`; const subject = `Ihr Mietvertrag für ${booking.eventLocation || 'Ihr Event'}`; const html = `

🎉 SaveTheMoment

Ihr Mietvertrag ist bereit!

Hallo ${booking.customerName},

vielen Dank für Ihre Buchung bei SaveTheMoment! Wir freuen uns sehr, Teil Ihres besonderen Anlasses zu sein.

📋 Buchungsdetails

Buchungsnummer: ${booking.bookingNumber}

Event-Datum: ${new Date(booking.eventDate).toLocaleDateString('de-DE', { day: '2-digit', month: 'long', year: 'numeric' })}

Location: ${booking.eventLocation || booking.eventAddress}

Fotobox: ${booking.photobox?.model || 'N/A'}

Im Anhang finden Sie Ihren Mietvertrag als PDF-Datei.

Nächste Schritte:

  1. Bitte lesen Sie den Vertrag sorgfältig durch
  2. Signieren Sie den Vertrag online oder drucken Sie ihn aus und senden Sie ihn zurück
  3. Nach Erhalt der Unterschrift ist Ihre Buchung verbindlich bestätigt
✍️ Vertrag online signieren

Alternativ können Sie den Vertrag auch ausdrucken, unterschreiben und uns per E-Mail oder Post zurücksenden.

Bei Fragen stehen wir Ihnen jederzeit gerne zur Verfügung!

Mit freundlichen Grüßen
Ihr SaveTheMoment Team

`.trim(); const text = ` Hallo ${booking.customerName}, vielen Dank für Ihre Buchung bei SaveTheMoment! Buchungsdetails: - Buchungsnummer: ${booking.bookingNumber} - Event-Datum: ${new Date(booking.eventDate).toLocaleDateString('de-DE')} - Location: ${booking.eventLocation || booking.eventAddress} Im Anhang finden Sie Ihren Mietvertrag als PDF-Datei. Sie können den Vertrag online signieren unter: ${signUrl} Oder drucken Sie ihn aus und senden Sie ihn uns zurück. Bei Fragen stehen wir Ihnen jederzeit zur Verfügung! Mit freundlichen Grüßen Ihr SaveTheMoment Team --- SaveTheMoment Fotoboxen E-Mail: info@savethemoment.photos Web: www.savethemoment.photos `.trim(); let pdfBuffer: Buffer; try { pdfBuffer = await readFile(path.join(process.cwd(), 'public', contractPdfPath)); } catch (error) { console.error('Failed to read contract PDF:', error); throw new Error('Contract PDF not found'); } return sendEmail({ to: booking.customerEmail, subject, text, html, attachments: [ { filename: `Mietvertrag_${booking.bookingNumber}.pdf`, content: pdfBuffer, }, ], }); } export async function sendBookingConfirmationEmail(booking: any) { const subject = `Buchungsbestätigung - ${booking.bookingNumber}`; const html = `

✅ Buchung bestätigt!

Hallo ${booking.customerName},

Ihre Buchung wurde erfolgreich bestätigt!

Buchungsdetails

Buchungsnummer: ${booking.bookingNumber}

Event-Datum: ${new Date(booking.eventDate).toLocaleDateString('de-DE')}

Location: ${booking.eventLocation || booking.eventAddress}

Wir freuen uns auf Ihr Event!

Mit freundlichen Grüßen
Ihr SaveTheMoment Team

`; const text = ` Hallo ${booking.customerName}, Ihre Buchung wurde erfolgreich bestätigt! Buchungsnummer: ${booking.bookingNumber} Event-Datum: ${new Date(booking.eventDate).toLocaleDateString('de-DE')} Location: ${booking.eventLocation || booking.eventAddress} Wir freuen uns auf Ihr Event! Mit freundlichen Grüßen Ihr SaveTheMoment Team `; return sendEmail({ to: booking.customerEmail, subject, text, html, }); }