37301-vm/contact.php
Flatlogic Bot 9bf0056520 test
2026-01-07 09:28:17 +00:00

126 lines
6.7 KiB
PHP

<?php
$pageTitle = 'Contact Us - Luxury Carpets';
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/mail/MailService.php';
$errors = [];
$success = false;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST['name'] ?? '');
$company = trim($_POST['company'] ?? '');
$email = trim($_POST['email'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$message = trim($_POST['message'] ?? '');
$source_page = $_SERVER['HTTP_REFERER'] ?? 'Direct';
if (empty($name)) {
$errors['name'] = 'Name is required.';
}
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors['email'] = 'A valid email is required.';
}
if (empty($message)) {
$errors['message'] = 'Message is required.';
}
if (empty($errors)) {
try {
$pdo = db();
$stmt = $pdo->prepare(
"INSERT INTO inquiries (name, company, email, phone, message, source_page) VALUES (?, ?, ?, ?, ?, ?)"
);
$stmt->execute([$name, $company, $email, $phone, $message, $source_page]);
// Send email notification
$admin_email = getenv('MAIL_TO') ?: 'admin@example.com'; // Fallback
$subject = "New Inquiry from " . htmlspecialchars($name);
$body_html = "<h1>New Inquiry</h1>
<p><strong>Name:</strong> " . htmlspecialchars($name) . "</p>
<p><strong>Company:</strong> " . htmlspecialchars($company) . "</p>
<p><strong>Email:</strong> " . htmlspecialchars($email) . "</p>
<p><strong>Phone:</strong> " . htmlspecialchars($phone) . "</p>
<p><strong>Message:</strong></p>
<p>" . nl2br(htmlspecialchars($message)) . "</p>
<p><strong>Source:</strong> " . htmlspecialchars($source_page) . "</p>";
$body_text = "New Inquiry\n\nName: " . $name . "\nCompany: " . $company . "\nEmail: " . $email . "\nPhone: " . $phone . "\nMessage: " . $message . "\nSource: " . $source_page;
// Using MailService
MailService::sendMail($admin_email, $subject, $body_html, $body_text, ['reply_to' => $email]);
$success = true;
} catch (PDOException $e) {
$errors['db'] = 'There was a problem saving your inquiry. Please try again later.';
error_log('Inquiry Error: ' . $e->getMessage());
} catch (Exception $e) {
$errors['mail'] = 'There was a problem sending the notification. Your inquiry was saved.';
error_log('Mail Error: ' . $e->getMessage());
}
}
}
include __DIR__ . '/includes/header.php';
?>
<main class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card border-0 shadow-sm">
<div class="card-body p-5">
<h1 class="text-center mb-4">Contact Us</h1>
<p class="text-center text-muted mb-5">We would love to hear from you. Please fill out this form and we will get in touch with you shortly.</p>
<?php if ($success): ?>
<div class="alert alert-success" role="alert">
<strong>Thank you!</strong> Your inquiry has been sent successfully.
</div>
<?php else: ?>
<?php if (!empty($errors['db']) || !empty($errors['mail'])): ?>
<div class="alert alert-danger" role="alert">
<?php echo htmlspecialchars($errors['db'] ?? $errors['mail']); ?>
</div>
<?php endif; ?>
<form action="contact.php" method="POST" novalidate>
<div class="mb-3">
<label for="name" class="form-label">Full Name</label>
<input type="text" class="form-control <?php echo isset($errors['name']) ? 'is-invalid' : ''; ?>" id="name" name="name" value="<?php echo htmlspecialchars($_POST['name'] ?? ''); ?>" required>
<?php if (isset($errors['name'])): ?>
<div class="invalid-feedback"><?php echo $errors['name']; ?></div>
<?php endif; ?>
</div>
<div class="mb-3">
<label for="company" class="form-label">Company Name</label>
<input type="text" class="form-control" id="company" name="company" value="<?php echo htmlspecialchars($_POST['company'] ?? ''); ?>">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email Address</label>
<input type="email" class="form-control <?php echo isset($errors['email']) ? 'is-invalid' : ''; ?>" id="email" name="email" value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>" required>
<?php if (isset($errors['email'])): ?>
<div class="invalid-feedback"><?php echo $errors['email']; ?></div>
<?php endif; ?>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone Number (Optional)</label>
<input type="tel" class="form-control" id="phone" name="phone" value="<?php echo htmlspecialchars($_POST['phone'] ?? ''); ?>">
</div>
<div class="mb-3">
<label for="message" class="form-label">Message</label>
<textarea class="form-control <?php echo isset($errors['message']) ? 'is-invalid' : ''; ?>" id="message" name="message" rows="5" required><?php echo htmlspecialchars($_POST['message'] ?? ''); ?></textarea>
<?php if (isset($errors['message'])): ?>
<div class="invalid-feedback"><?php echo $errors['message']; ?></div>
<?php endif; ?>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary btn-lg">Send Inquiry</button>
</div>
</form>
<?php endif; ?>
</div>
</div>
</div>
</div>
</main>
<?php include __DIR__ . '/includes/footer.php'; ?>