import type { APIRoute } from 'astro'; import { loadWaasConfigs } from '../../utils/configLoader'; import nodemailer from 'nodemailer'; export const POST: APIRoute = async ({ request }) => { try { const data = await request.formData(); const name = data.get('name')?.toString() || ''; const email = data.get('email')?.toString() || ''; const message = data.get('message')?.toString() || ''; if (!name || !email || !message) { return new Response(JSON.stringify({ success: false, message: 'Bitte füllen Sie alle Pflichtfelder aus.' }), { status: 400, headers: { 'Content-Type': 'application/json' } }); } const { smtpConfig, siteConfig } = loadWaasConfigs(Astro.request.headers.get('user-agent') ?? ''); if (!smtpConfig.isConfigured || smtpConfig.host === 'smtp.placeholder.local') { console.log('[WaaS Contact API] (Demo Mode) Received Submission:', { name, email, message }); return new Response(JSON.stringify({ success: true, mode: 'placeholder', message: 'Anfrage erfolgreich empfangen! (Demo-Modus)' }), { status: 200, headers: { 'Content-Type': 'application/json' } }); } const transporter = nodemailer.createTransport({ host: smtpConfig.host, port: smtpConfig.port, secure: smtpConfig.secure, auth: { user: smtpConfig.user, pass: smtpConfig.pass } }); const sender = smtpConfig.fromName + ' ' + smtpConfig.fromEmail; await transporter.sendMail({ from: sender, to: siteConfig.contact.recipientEmail, replyTo: email, subject: '[Webseite] Neue Nachricht von ' + name, text: 'Name: ' + name + '\nEmail: ' + email + '\n\nNachricht:\n' + message }); return new Response(JSON.stringify({ success: true, message: 'Vielen Dank für Ihre Anfrage! Wir melden uns umgehend.' }), { status: 200, headers: { 'Content-Type': 'application/json' } }); } catch (err: any) { return new Response(JSON.stringify({ success: false, message: 'Fehler beim Senden: ' + err.message }), { status: 500, headers: { 'Content-Type': 'application/json' } }); } };