Fix: Use AgentMail API instead of mail(), fix reply_to format
All checks were successful
Deploy Feature Branch to Test / deploy (push) Successful in 24s
All checks were successful
Deploy Feature Branch to Test / deploy (push) Successful in 24s
This commit is contained in:
44
index.php
44
index.php
@@ -17,6 +17,40 @@ function containsHeaderInjection(string $value): bool
|
|||||||
return (bool) preg_match('/[\r\n]/', $value);
|
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 ---
|
// --- Form processing ---
|
||||||
$formErrors = [];
|
$formErrors = [];
|
||||||
$formSuccess = false;
|
$formSuccess = false;
|
||||||
@@ -69,9 +103,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$formErrors[] = 'Bitte warten Sie einen Moment vor der nächsten Anfrage.';
|
$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)) {
|
if (empty($formErrors)) {
|
||||||
$to = 'mki@kies-media.de';
|
|
||||||
$subject = 'Kontaktanfrage: ' . $formData['interest'];
|
$subject = 'Kontaktanfrage: ' . $formData['interest'];
|
||||||
$body = "Von: {$formData['fname']} {$formData['lname']}\n"
|
$body = "Von: {$formData['fname']} {$formData['lname']}\n"
|
||||||
. "E-Mail: {$formData['email']}\n";
|
. "E-Mail: {$formData['email']}\n";
|
||||||
@@ -81,12 +114,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$body .= "Anliegen: {$formData['interest']}\n\n"
|
$body .= "Anliegen: {$formData['interest']}\n\n"
|
||||||
. $formData['message'];
|
. $formData['message'];
|
||||||
|
|
||||||
$headers = "From: {$formData['email']}\r\n";
|
$mailSent = sendContactEmail($subject, $body, $formData['email'], $formData['fname'] . ' ' . $formData['lname']);
|
||||||
$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);
|
|
||||||
|
|
||||||
if ($mailSent) {
|
if ($mailSent) {
|
||||||
$formSuccess = true;
|
$formSuccess = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user