75 lines
2.7 KiB
PHP
75 lines
2.7 KiB
PHP
<?php
|
|
require_once __DIR__ . '/mail/MailService.php';
|
|
|
|
$success_message = '';
|
|
$error_message = '';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$name = strip_tags(trim($_POST["name"]));
|
|
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
|
|
$message = strip_tags(trim($_POST["message"]));
|
|
|
|
if (empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$error_message = "Please fill out all fields and provide a valid email address.";
|
|
} else {
|
|
$to = getenv('MAIL_TO') ?: null;
|
|
$subject = "New contact from " . $name;
|
|
|
|
$email_body = "You have received a new message from your website contact form.\n\n";
|
|
$email_body .= "Here are the details:\n";
|
|
$email_body .= "Name: $name\n";
|
|
$email_body .= "Email: $email\n";
|
|
$email_body .= "Message:\n$message\n";
|
|
|
|
$res = MailService::sendContactMessage($name, $email, $message, $to, $subject);
|
|
|
|
if (!empty($res['success'])) {
|
|
$success_message = "Thank you for your message. It has been sent.";
|
|
} else {
|
|
$error_message = "Sorry, there was an error sending your message. Please try again later.";
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Contact Us</title>
|
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
|
</head>
|
|
<body>
|
|
<div class="container mt-5">
|
|
<h1>Contact Us</h1>
|
|
<p>Fill out the form below to get in touch with us.</p>
|
|
|
|
<?php if(!empty($success_message)): ?>
|
|
<div class="alert alert-success"><?php echo $success_message; ?></div>
|
|
<?php endif; ?>
|
|
<?php if(!empty($error_message)): ?>
|
|
<div class="alert alert-danger"><?php echo $error_message; ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form action="contact.php" method="post">
|
|
<div class="form-group">
|
|
<label for="name">Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="email">Email</label>
|
|
<input type="email" class="form-control" id="email" name="email" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="message">Message</label>
|
|
<textarea class="form-control" id="message" name="message" rows="5" required></textarea>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Send Message</button>
|
|
</form>
|
|
<p class="mt-3">
|
|
<a href="/">Back to Home</a>
|
|
</p>
|
|
</div>
|
|
</body>
|
|
</html>
|