import { serve } from "https://deno.land/std@0.168.0/http/server.ts"; const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', }; serve(async (req) => { // Handle CORS preflight requests if (req.method === 'OPTIONS') { return new Response('ok', { headers: corsHeaders }); } try { const { name, email, phone, subject, message, to } = await req.json(); // Validierung if (!name || !email || !subject || !message || !to) { return new Response( JSON.stringify({ error: 'Missing required fields' }), { status: 400, headers: { ...corsHeaders, 'Content-Type': 'application/json' } } ); } // E-Mail-Inhalt erstellen const emailContent = ` Neue Kontaktanfrage von: ${name} E-Mail: ${email} Telefon: ${phone || 'Keine Angabe'} Betreff: ${subject} Nachricht: ${message} `.trim(); // E-Mail über Resend API senden (oder einen anderen E-Mail-Dienst) const resendResponse = await fetch('https://api.resend.com/emails', { method: 'POST', headers: { 'Authorization': `Bearer ${Deno.env.get('RESEND_API_KEY')}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ from: 'kontakt@finanzen-mizera.de', to: [to], subject: `Kontaktanfrage: ${subject}`, html: `

Neue Kontaktanfrage

Von: ${name} (${email})

Telefon: ${phone || 'Keine Angabe'}

Betreff: ${subject}


Nachricht:

${message.replace(/\n/g, '
')}


Gesendet über das Kontaktformular auf finanzen-mizera.de

`, text: emailContent, }), }); if (!resendResponse.ok) { const errorData = await resendResponse.text(); console.error('Resend API Error:', errorData); return new Response( JSON.stringify({ error: 'Failed to send email' }), { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } } ); } const responseData = await resendResponse.json(); return new Response( JSON.stringify({ success: true, data: responseData }), { status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } } ); } catch (error) { console.error('Function error:', error); return new Response( JSON.stringify({ error: error.message }), { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } } ); } });