implemented step 18 by Gemini
- Contact formular and emailer script
This commit is contained in:
82
src/Templates/emailer.php
Normal file
82
src/Templates/emailer.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* Universal Form Handler for WebWizard exported sites.
|
||||
* Handles both SMTP mailing and local file storage.
|
||||
*/
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
$config = [];
|
||||
if (file_exists('config.json')) {
|
||||
$config = json_decode(file_get_contents('config.json'), true);
|
||||
}
|
||||
|
||||
function sendResponse(bool $success, string $message) {
|
||||
echo json_encode(['success' => $success, 'message' => $message]);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
sendResponse(false, 'Method not allowed.');
|
||||
}
|
||||
|
||||
// Honeypot spam protection
|
||||
if (!empty($_POST['website'])) {
|
||||
sendResponse(true, 'Spam detected.'); // Fake success for bots
|
||||
}
|
||||
|
||||
$name = trim($_POST['name'] ?? '');
|
||||
$email = trim($_POST['email'] ?? '');
|
||||
$message = trim($_POST['message'] ?? '');
|
||||
|
||||
if (!$name || !$email || !$message) {
|
||||
sendResponse(false, 'Prosím vyplňte všetky povinné polia.');
|
||||
}
|
||||
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
sendResponse(false, 'Neplatná emailová adresa.');
|
||||
}
|
||||
|
||||
$mode = $config['form_mode'] ?? 'local';
|
||||
|
||||
if ($mode === 'local') {
|
||||
// Save to messages directory
|
||||
$msgDir = 'messages';
|
||||
if (!is_dir($msgDir)) {
|
||||
mkdir($msgDir, 0777, true);
|
||||
}
|
||||
|
||||
$msgId = date('Ymd-His') . '-' . bin2hex(random_bytes(4));
|
||||
$msgData = [
|
||||
'id' => $msgId,
|
||||
'timestamp' => date('c'),
|
||||
'sender_name' => $name,
|
||||
'sender_email' => $email,
|
||||
'message' => $message
|
||||
];
|
||||
|
||||
if (file_put_contents($msgDir . '/' . $msgId . '.json', json_encode($msgData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE))) {
|
||||
sendResponse(true, 'Vaša správa bola úspešne uložená.');
|
||||
} else {
|
||||
sendResponse(false, 'Nepodarilo sa uložiť správu.');
|
||||
}
|
||||
} else {
|
||||
// SMTP / Mail mode
|
||||
$to = $config['smtp']['recipient'] ?? '';
|
||||
if (!$to) {
|
||||
sendResponse(false, 'Cieľový email nie je nakonfigurovaný.');
|
||||
}
|
||||
|
||||
$subject = "Nová správa z webu: " . ($config['site_name'] ?? 'WebWizard');
|
||||
$body = "Meno: $name\nEmail: $email\n\nSpráva:\n$message";
|
||||
$headers = "From: webwizard@{$_SERVER['HTTP_HOST']}\r\n" .
|
||||
"Reply-To: $email\r\n" .
|
||||
"X-Mailer: PHP/" . phpversion();
|
||||
|
||||
// Using standard mail() as fallback for MVP if no complex SMTP is provided
|
||||
if (mail($to, $subject, $body, $headers)) {
|
||||
sendResponse(true, 'Vaša správa bola úspešne odoslaná.');
|
||||
} else {
|
||||
sendResponse(false, 'Nepodarilo sa odoslať email.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user