Files
klz-cables.com/app/[locale]/api/contact/route.ts
2025-12-28 23:28:31 +01:00

72 lines
2.1 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { Resend } from 'resend';
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { name, email, message, locale } = body;
// Validate required fields
if (!name || !email || !message) {
return NextResponse.json(
{ error: 'Missing required fields' },
{ status: 400 }
);
}
// Validate email format
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
return NextResponse.json(
{ error: 'Invalid email format' },
{ status: 400 }
);
}
// Check if Resend API key is configured
if (!process.env.RESEND_API_KEY) {
return NextResponse.json(
{ error: 'Email service not configured' },
{ status: 500 }
);
}
const resend = new Resend(process.env.RESEND_API_KEY);
// Send email via Resend
const { data, error } = await resend.emails.send({
from: 'KLZ Cables <contact@klz-cables.com>',
to: ['info@klz-cables.com'],
subject: locale === 'de' ? 'Neue Kontaktanfrage' : 'New Contact Inquiry',
html: `
<h2>${locale === 'de' ? 'Neue Kontaktanfrage' : 'New Contact Inquiry'}</h2>
<p><strong>${locale === 'de' ? 'Name' : 'Name'}:</strong> ${name}</p>
<p><strong>${locale === 'de' ? 'E-Mail' : 'Email'}:</strong> ${email}</p>
<p><strong>${locale === 'de' ? 'Nachricht' : 'Message'}:</strong></p>
<p>${message}</p>
<hr>
<p><small>${locale === 'de' ? 'Gesendet über' : 'Sent via'} KLZ Cables Website</small></p>
`,
});
if (error) {
console.error('Resend error:', error);
return NextResponse.json(
{ error: 'Failed to send email' },
{ status: 500 }
);
}
return NextResponse.json(
{ success: true, data },
{ status: 200 }
);
} catch (error) {
console.error('Contact API error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}