Files
website/contact.php
T

82 lines
2.6 KiB
PHP

<?php
/**
* BvPS contactformulier — contact.php
* Zet dit bestand in dezelfde map als index.html op uw Strato-hosting.
*
* Pas de constante ONTVANGER aan naar het gewenste e-mailadres.
*/
define('ONTVANGER', 'info@bvps.nl');
define('ONDERWERP_PREFIX', '[BvPS Website] ');
header('Content-Type: application/json');
// Alleen POST-verzoeken accepteren
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
exit;
}
// JSON body inlezen
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
echo json_encode(['success' => false, 'error' => 'Geen geldige invoer']);
exit;
}
// Invoer ophalen en opschonen
$naam = isset($input['naam']) ? trim(strip_tags($input['naam'])) : '';
$email = isset($input['email']) ? trim(strip_tags($input['email'])) : '';
$org = isset($input['org']) ? trim(strip_tags($input['org'])) : '';
$onderwerp = isset($input['onderwerp']) ? trim(strip_tags($input['onderwerp'])) : '';
$bericht = isset($input['bericht']) ? trim(strip_tags($input['bericht'])) : '';
// Verplichte velden valideren
if (empty($naam) || empty($email) || empty($bericht)) {
echo json_encode(['success' => false, 'error' => 'Verplichte velden ontbreken']);
exit;
}
// E-mailadres valideren
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo json_encode(['success' => false, 'error' => 'Ongeldig e-mailadres']);
exit;
}
// Onderwerpregel samenstellen
$mail_onderwerp = ONDERWERP_PREFIX . ($onderwerp ?: 'Nieuw bericht via website');
// E-mailinhoud opbouwen
$mail_body = "Nieuw bericht via het contactformulier op bvps.nl\n";
$mail_body .= str_repeat('-', 50) . "\n\n";
$mail_body .= "Naam: $naam\n";
$mail_body .= "E-mailadres: $email\n";
if ($org) {
$mail_body .= "Organisatie: $org\n";
}
if ($onderwerp) {
$mail_body .= "Onderwerp: $onderwerp\n";
}
$mail_body .= "\nBericht:\n$bericht\n";
$mail_body .= "\n" . str_repeat('-', 50) . "\n";
$mail_body .= "Verzonden via bvps.nl op " . date('d-m-Y \o\m H:i') . "\n";
// E-mailheaders instellen
$headers = "From: website@bvps.nl\r\n";
$headers .= "Reply-To: $naam <$email>\r\n";
$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
// E-mail versturen
$verstuurd = mail(ONTVANGER, $mail_onderwerp, $mail_body, $headers);
if ($verstuurd) {
echo json_encode(['success' => true]);
} else {
http_response_code(500);
echo json_encode(['success' => false, 'error' => 'Verzenden mislukt']);
}
?>