t-r.de/content/misc/contact.php
2025-11-21 08:58:14 +01:00

55 lines
1.6 KiB
PHP

<?php
// inspired by: https://www.unixdude.net/posts/2017/Nov/29/pelican-contact-form/
// make sure method is POST
if ( $_SERVER['REQUEST_METHOD'] != 'POST' ) {
header('Location: /');
exit;
}
// make sure all required fields are set
// HTML already validates this, but bots sometimes call the script directly
if ( $_POST['name'] == '' or $_POST['email'] == '' or $_POST['nachricht'] == '') {
header('Location: /');
exit;
}
// The 'address' form field is in the code but doesn't get rendered on the page. The message will only get sent if the field is empty, thus weeding out bots that will just fill out any form field
if ( $_POST['address'] != '' ) {
header('Location: /');
exit;
}
$webroot = $_SERVER['DOCUMENT_ROOT'];
require($webroot . '/functions.php');
$successURL = '/success.html';
$errorURL = '/error.html';
$err = 'Nachrichtenversand fehlgeschlagen';
$body = [
'Diese Nachricht wurde soeben durch das Kontaktformular auf t-r.de übermittelt:',
"Name: {$_POST['name']}",
"Emailadresse: {$_POST['email']}",
"Nachricht: {$_POST['nachricht']}"
];
$mailContents = [
'subject' => 'Diese Nachricht kam ueber das Kontaktformular von t-r.de',
'bodyText' => implode("\n\n", $body),
'fromAddress' => $general['notificationAddress'],
'fromName' => 'Tobias Radloffs Kontaktformular',
'replyTo' => $_POST['email'],
'name' => $_POST['name']
];
try {
SendEmail($general['notificationAddress'], $mailContents);
} catch (Exception $e) {
GracefulExit($errorURL, "{$err}: {$e->getMessage()}");
}
// success
GracefulExit($successURL, "Deine Nachricht wurde erfolgreich verschickt und ist jetzt auf dem Weg zu mir.");
?>