cpanel-site-hosting / contact.php
sacredfire01's picture
I need this to work on cpanel hosting
ed28b39 verified
Raw History Blame Contribute Delete
1.93 kB
<?php
// cPanel compatible contact form processor
// Place this in the same directory as your index.html
// Configure your email here
$to = "your-email@example.com"; // <-- CHANGE THIS TO YOUR EMAIL
$subject = "New Contact Form Submission";
// Check if form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Collect and sanitize form data
$name = filter_var(trim($_POST["name"]), FILTER_SANITIZE_STRING);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = filter_var(trim($_POST["message"]), FILTER_SANITIZE_STRING);
// Validation
$errors = [];
if (empty($name)) {
$errors[] = "Name is required";
}
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Valid email is required";
}
if (empty($message)) {
$errors[] = "Message is required";
}
// If no errors, send email
if (empty($errors)) {
$email_subject = "New Message from $name";
$email_body = "Name: $name\n";
$email_body .= "Email: $email\n\n";
$email_body .= "Message:\n$message\n";
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
// Attempt to send email
if (mail($to, $email_subject, $email_body, $headers)) {
// Success - redirect back with success message
header("Location: index.html?status=success#contact");
exit();
} else {
// Failed to send
header("Location: index.html?status=error#contact");
exit();
}
} else {
// Validation errors
header("Location: index.html?status=validation#contact");
exit();
}
} else {
// Not a POST request
header("Location: index.html");
exit();
}
?>