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;
|
||||
?>
|
||||
Reference in New Issue
Block a user