Add functional contact form with PHP backend and SMTP integration
- Create contact-handler.php with SMTP configuration for mail.romfast.ro - Update contact.html with form action, validation, and JavaScript messaging - Implement email delivery to office@romfast.ro with proper headers - Add success/error message handling with clean URL management - Include form validation and user feedback improvements 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
165
contact-handler.php
Normal file
165
contact-handler.php
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
<?php
|
||||||
|
// Configurare pentru afișarea erorilor în timpul dezvoltării
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
ini_set('display_errors', 1);
|
||||||
|
|
||||||
|
// Verifică dacă formularul a fost trimis prin POST
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||||
|
header('Location: menu/contact.html');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configurare SMTP
|
||||||
|
$smtp_server = 'mail.romfast.ro';
|
||||||
|
$smtp_port = 587; // Port standard pentru STARTTLS
|
||||||
|
$smtp_username = 'ups@romfast.ro';
|
||||||
|
$smtp_password = '#Ups2020#';
|
||||||
|
$from_email = 'contact@romfast.ro';
|
||||||
|
$to_email = 'office@romfast.ro';
|
||||||
|
|
||||||
|
// Preluarea și validarea datelor din formular
|
||||||
|
$nume = trim($_POST['nume'] ?? '');
|
||||||
|
$email = trim($_POST['email'] ?? '');
|
||||||
|
$telefon = trim($_POST['telefon'] ?? '');
|
||||||
|
$subiect = trim($_POST['subiect'] ?? '');
|
||||||
|
$mesaj = trim($_POST['mesaj'] ?? '');
|
||||||
|
|
||||||
|
// Validări de bază
|
||||||
|
$errors = [];
|
||||||
|
|
||||||
|
if (empty($nume)) {
|
||||||
|
$errors[] = 'Numele este obligatoriu.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||||
|
$errors[] = 'Adresa de email este obligatorie și trebuie să fie validă.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($subiect)) {
|
||||||
|
$errors[] = 'Subiectul este obligatoriu.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($mesaj)) {
|
||||||
|
$errors[] = 'Mesajul este obligatoriu.';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dacă sunt erori, redirecționează înapoi la formular
|
||||||
|
if (!empty($errors)) {
|
||||||
|
$error_message = implode('<br>', $errors);
|
||||||
|
header('Location: menu/contact.html?error=' . urlencode($error_message));
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Maparea valorilor pentru subiect
|
||||||
|
$subiect_map = [
|
||||||
|
'informatii-roa' => 'Informații despre ROA',
|
||||||
|
'suport-tehnic' => 'Suport tehnic',
|
||||||
|
'implementare' => 'Implementare soluție',
|
||||||
|
'demo' => 'Solicitare demo',
|
||||||
|
'altele' => 'Altele'
|
||||||
|
];
|
||||||
|
|
||||||
|
$subiect_text = $subiect_map[$subiect] ?? $subiect;
|
||||||
|
|
||||||
|
// Construirea mesajului email
|
||||||
|
$email_subject = 'Contact formular site - ' . $subiect_text;
|
||||||
|
$email_body = "
|
||||||
|
Mesaj nou de pe site-ul Romfast.ro
|
||||||
|
|
||||||
|
Detalii contact:
|
||||||
|
- Nume: {$nume}
|
||||||
|
- Email: {$email}
|
||||||
|
- Telefon: " . (!empty($telefon) ? $telefon : 'Nu a fost specificat') . "
|
||||||
|
- Subiect: {$subiect_text}
|
||||||
|
|
||||||
|
Mesaj:
|
||||||
|
{$mesaj}
|
||||||
|
|
||||||
|
---
|
||||||
|
Trimis de pe formularul de contact al site-ului Romfast.ro
|
||||||
|
IP: {$_SERVER['REMOTE_ADDR']}
|
||||||
|
Data: " . date('d.m.Y H:i:s') . "
|
||||||
|
";
|
||||||
|
|
||||||
|
// Headers pentru email
|
||||||
|
$headers = [
|
||||||
|
'From' => $from_email,
|
||||||
|
'Reply-To' => $email,
|
||||||
|
'X-Mailer' => 'PHP/' . phpversion(),
|
||||||
|
'MIME-Version' => '1.0',
|
||||||
|
'Content-Type' => 'text/plain; charset=UTF-8'
|
||||||
|
];
|
||||||
|
|
||||||
|
// Funcție pentru trimiterea email-ului prin SMTP
|
||||||
|
function sendEmailSMTP($to, $subject, $body, $headers, $smtp_config) {
|
||||||
|
// Încercăm să folosim PHPMailer dacă este disponibil
|
||||||
|
if (class_exists('PHPMailer\PHPMailer\PHPMailer')) {
|
||||||
|
require_once 'vendor/autoload.php';
|
||||||
|
|
||||||
|
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Configurare server SMTP
|
||||||
|
$mail->isSMTP();
|
||||||
|
$mail->Host = $smtp_config['server'];
|
||||||
|
$mail->SMTPAuth = true;
|
||||||
|
$mail->Username = $smtp_config['username'];
|
||||||
|
$mail->Password = $smtp_config['password'];
|
||||||
|
$mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
|
||||||
|
$mail->Port = $smtp_config['port'];
|
||||||
|
$mail->CharSet = 'UTF-8';
|
||||||
|
|
||||||
|
// Setări destinatar și expeditor
|
||||||
|
$mail->setFrom($smtp_config['from'], 'Formular Contact Romfast');
|
||||||
|
$mail->addAddress($to);
|
||||||
|
$mail->addReplyTo($headers['Reply-To']);
|
||||||
|
|
||||||
|
// Conținut
|
||||||
|
$mail->isHTML(false);
|
||||||
|
$mail->Subject = $subject;
|
||||||
|
$mail->Body = $body;
|
||||||
|
|
||||||
|
$mail->send();
|
||||||
|
return true;
|
||||||
|
} catch (Exception $e) {
|
||||||
|
error_log("Eroare PHPMailer: " . $mail->ErrorInfo);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Fallback la funcția mail() cu configurare SMTP prin ini_set
|
||||||
|
ini_set('SMTP', $smtp_config['server']);
|
||||||
|
ini_set('smtp_port', $smtp_config['port']);
|
||||||
|
ini_set('sendmail_from', $smtp_config['from']);
|
||||||
|
|
||||||
|
$header_string = '';
|
||||||
|
foreach ($headers as $key => $value) {
|
||||||
|
$header_string .= $key . ': ' . $value . "\r\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return mail($to, $subject, $body, $header_string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configurare pentru SMTP
|
||||||
|
$smtp_config = [
|
||||||
|
'server' => $smtp_server,
|
||||||
|
'port' => $smtp_port,
|
||||||
|
'username' => $smtp_username,
|
||||||
|
'password' => $smtp_password,
|
||||||
|
'from' => $from_email
|
||||||
|
];
|
||||||
|
|
||||||
|
// Încearcă să trimită emailul
|
||||||
|
$email_sent = sendEmailSMTP($to_email, $email_subject, $email_body, $headers, $smtp_config);
|
||||||
|
|
||||||
|
if ($email_sent) {
|
||||||
|
// Succes - redirecționează cu mesaj de confirmare
|
||||||
|
header('Location: menu/contact.html?success=1');
|
||||||
|
} else {
|
||||||
|
// Eroare la trimiterea emailului
|
||||||
|
error_log("Eroare la trimiterea emailului de contact de la: {$email}");
|
||||||
|
header('Location: menu/contact.html?error=' . urlencode('A apărut o eroare la trimiterea mesajului. Vă rugăm să încercați din nou sau să ne contactați direct.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
exit;
|
||||||
|
?>
|
||||||
@@ -136,15 +136,18 @@
|
|||||||
</div>
|
</div>
|
||||||
<h2 class="text-2xl font-semibold mb-6 professional-text-primary text-center">Trimite-ne un Mesaj</h2>
|
<h2 class="text-2xl font-semibold mb-6 professional-text-primary text-center">Trimite-ne un Mesaj</h2>
|
||||||
|
|
||||||
<form class="space-y-6">
|
<!-- Mesaje de succes/eroare -->
|
||||||
|
<div id="form-messages"></div>
|
||||||
|
|
||||||
|
<form class="space-y-6" action="../contact-handler.php" method="POST">
|
||||||
<div>
|
<div>
|
||||||
<label for="nume" class="block text-sm font-medium professional-text-primary mb-2">Nume complet</label>
|
<label for="nume" class="block text-sm font-medium professional-text-primary mb-2">Nume complet</label>
|
||||||
<input type="text" id="nume" name="nume" class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors" placeholder="Numele dumneavoastră">
|
<input type="text" id="nume" name="nume" required class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors" placeholder="Numele dumneavoastră">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label for="email" class="block text-sm font-medium professional-text-primary mb-2">Adresa de e-mail</label>
|
<label for="email" class="block text-sm font-medium professional-text-primary mb-2">Adresa de e-mail</label>
|
||||||
<input type="email" id="email" name="email" class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors" placeholder="exemplu@email.com">
|
<input type="email" id="email" name="email" required class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors" placeholder="exemplu@email.com">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
@@ -154,9 +157,9 @@
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label for="subiect" class="block text-sm font-medium professional-text-primary mb-2">Subiect</label>
|
<label for="subiect" class="block text-sm font-medium professional-text-primary mb-2">Subiect</label>
|
||||||
<select id="subiect" name="subiect" class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors">
|
<select id="subiect" name="subiect" required class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors">
|
||||||
<option value="">Selectați subiectul</option>
|
<option value="">Selectați subiectul</option>
|
||||||
<option value="informaii-roa">Informații despre ROA</option>
|
<option value="informatii-roa">Informații despre ROA</option>
|
||||||
<option value="suport-tehnic">Suport tehnic</option>
|
<option value="suport-tehnic">Suport tehnic</option>
|
||||||
<option value="implementare">Implementare soluție</option>
|
<option value="implementare">Implementare soluție</option>
|
||||||
<option value="demo">Solicitare demo</option>
|
<option value="demo">Solicitare demo</option>
|
||||||
@@ -166,7 +169,7 @@
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label for="mesaj" class="block text-sm font-medium professional-text-primary mb-2">Mesajul dumneavoastră</label>
|
<label for="mesaj" class="block text-sm font-medium professional-text-primary mb-2">Mesajul dumneavoastră</label>
|
||||||
<textarea id="mesaj" name="mesaj" rows="5" class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors resize-none" placeholder="Descrieți cererea dumneavoastră în detaliu..."></textarea>
|
<textarea id="mesaj" name="mesaj" rows="5" required class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors resize-none" placeholder="Descrieți cererea dumneavoastră în detaliu..."></textarea>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button type="submit" class="professional-btn-primary px-8 py-4 text-lg w-full">
|
<button type="submit" class="professional-btn-primary px-8 py-4 text-lg w-full">
|
||||||
@@ -255,6 +258,81 @@
|
|||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<!-- Contact Form Messages JavaScript -->
|
||||||
|
<script>
|
||||||
|
// Afișarea mesajelor de succes/eroare
|
||||||
|
function showMessage(message, type) {
|
||||||
|
const messagesDiv = document.getElementById('form-messages');
|
||||||
|
const alertClass = type === 'success' ? 'bg-green-100 border-green-400 text-green-700' : 'bg-red-100 border-red-400 text-red-700';
|
||||||
|
|
||||||
|
messagesDiv.innerHTML = `
|
||||||
|
<div class="border-l-4 p-4 mb-6 rounded ${alertClass}" role="alert">
|
||||||
|
<div class="flex">
|
||||||
|
<div class="flex-shrink-0">
|
||||||
|
<i data-lucide="${type === 'success' ? 'check-circle' : 'alert-circle'}" class="w-5 h-5"></i>
|
||||||
|
</div>
|
||||||
|
<div class="ml-3">
|
||||||
|
<p class="text-sm">${message}</p>
|
||||||
|
</div>
|
||||||
|
<div class="ml-auto pl-3">
|
||||||
|
<div class="-mx-1.5 -my-1.5">
|
||||||
|
<button type="button" class="inline-flex rounded-md p-1.5 focus:outline-none focus:ring-2 focus:ring-offset-2" onclick="this.parentElement.parentElement.parentElement.parentElement.remove()">
|
||||||
|
<i data-lucide="x" class="w-4 h-4"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
// Reinitializează iconițele Lucide pentru mesajul nou
|
||||||
|
if (typeof lucide !== 'undefined') {
|
||||||
|
lucide.createIcons();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scroll către mesaj
|
||||||
|
messagesDiv.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verifică parametrii URL pentru mesaje
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
const urlParams = new URLSearchParams(window.location.search);
|
||||||
|
|
||||||
|
if (urlParams.has('success')) {
|
||||||
|
showMessage('Mesajul dumneavoastră a fost trimis cu succes! Vă vom răspunde în cel mai scurt timp posibil.', 'success');
|
||||||
|
|
||||||
|
// Curăță URL-ul
|
||||||
|
const newUrl = window.location.pathname;
|
||||||
|
window.history.replaceState({}, '', newUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (urlParams.has('error')) {
|
||||||
|
const errorMessage = urlParams.get('error');
|
||||||
|
showMessage(errorMessage, 'error');
|
||||||
|
|
||||||
|
// Curăță URL-ul
|
||||||
|
const newUrl = window.location.pathname;
|
||||||
|
window.history.replaceState({}, '', newUrl);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Validare suplimentară la submit
|
||||||
|
document.querySelector('form').addEventListener('submit', function(e) {
|
||||||
|
const submitBtn = this.querySelector('button[type="submit"]');
|
||||||
|
const originalText = submitBtn.innerHTML;
|
||||||
|
|
||||||
|
// Schimbă textul butonului în timpul trimiterii
|
||||||
|
submitBtn.innerHTML = '<span>Se trimite...</span>';
|
||||||
|
submitBtn.disabled = true;
|
||||||
|
|
||||||
|
// Resetează butonul după 5 secunde (în caz de eroare)
|
||||||
|
setTimeout(() => {
|
||||||
|
submitBtn.innerHTML = originalText;
|
||||||
|
submitBtn.disabled = false;
|
||||||
|
}, 5000);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
<!-- Professional Theme JavaScript -->
|
<!-- Professional Theme JavaScript -->
|
||||||
<script src="../professional-theme.js"></script>
|
<script src="../professional-theme.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user