Files
WebWizard/src/Templates/emailer.php

83 lines
2.2 KiB
PHP

<?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.php')) {
$config = include 'config.php';
}
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.');
}
}