2026-01-25 01:42:29 +01:00

87 lines
2.6 KiB
TypeScript

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: `
<h2>Neue Kontaktanfrage</h2>
<p><strong>Von:</strong> ${name} (${email})</p>
<p><strong>Telefon:</strong> ${phone || 'Keine Angabe'}</p>
<p><strong>Betreff:</strong> ${subject}</p>
<hr>
<h3>Nachricht:</h3>
<p>${message.replace(/\n/g, '<br>')}</p>
<hr>
<p><small>Gesendet über das Kontaktformular auf finanzen-mizera.de</small></p>
`,
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' } }
);
}
});