diff --git a/index.php b/index.php index c1c1587..ba03de4 100644 --- a/index.php +++ b/index.php @@ -17,6 +17,40 @@ function containsHeaderInjection(string $value): bool return (bool) preg_match('/[\r\n]/', $value); } +// --- Send email via AgentMail API --- +function sendContactEmail(string $subject, string $body, string $replyToEmail, string $replyToName): bool +{ + $apiKey = getenv('AGENTMAIL_API_KEY') ?: ''; + if ($apiKey === '') { + return false; + } + + $apiUrl = 'https://api.agentmail.to/v0/inboxes/max-kies-media-ai-assistent@agentmail.to/messages/send'; + $payload = json_encode([ + 'to' => ['mki@kies-media.de'], + 'subject' => $subject, + 'text' => $body, + 'reply_to' => $replyToEmail, + ]); + + $ch = curl_init($apiUrl); + curl_setopt_array($ch, [ + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $payload, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => 10, + CURLOPT_HTTPHEADER => [ + 'Authorization: Bearer ' . $apiKey, + 'Content-Type: application/json', + ], + ]); + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + return $httpCode >= 200 && $httpCode < 300; +} + // --- Form processing --- $formErrors = []; $formSuccess = false; @@ -69,9 +103,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $formErrors[] = 'Bitte warten Sie einen Moment vor der nächsten Anfrage.'; } - // Send email if no errors + // Send email via AgentMail API if no errors if (empty($formErrors)) { - $to = 'mki@kies-media.de'; $subject = 'Kontaktanfrage: ' . $formData['interest']; $body = "Von: {$formData['fname']} {$formData['lname']}\n" . "E-Mail: {$formData['email']}\n"; @@ -81,12 +114,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $body .= "Anliegen: {$formData['interest']}\n\n" . $formData['message']; - $headers = "From: {$formData['email']}\r\n"; - $headers .= "Reply-To: {$formData['email']}\r\n"; - $headers .= "Content-Type: text/plain; charset=UTF-8\r\n"; - $headers .= "X-Mailer: PHP/" . phpversion(); - - $mailSent = mail($to, $subject, $body, $headers); + $mailSent = sendContactEmail($subject, $body, $formData['email'], $formData['fname'] . ' ' . $formData['lname']); if ($mailSent) { $formSuccess = true;