// Error reporting for debugging (disable in production)
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Define variables and initialize with empty values
$name = $email = $phone = $subject = $message = $contactMethod = "";
$name_err = $email_err = $subject_err = $message_err = "";
$success_msg = "";
// Processing form data when submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate Name
if (empty(trim($_POST["name"]))) {
$name_err = "Please enter your name.";
} else {
$name = trim($_POST["name"]);
}
// Validate Email
if (empty(trim($_POST["email"]))) {
$email_err = "Please enter your email address.";
} elseif (!filter_var(trim($_POST["email"]), FILTER_VALIDATE_EMAIL)) {
$email_err = "Invalid email format.";
} else {
$email = trim($_POST["email"]);
}
// Phone (optional)
$phone = trim($_POST["phone"]);
// Validate Subject
if (empty(trim($_POST["subject"]))) {
$subject_err = "Please enter a subject.";
} else {
$subject = trim($_POST["subject"]);
}
// Validate Message
if (empty(trim($_POST["message"]))) {
$message_err = "Please enter your message.";
} else {
$message = trim($_POST["message"]);
}
// Contact Method
$contactMethod = isset($_POST["contactMethod"]) ? $_POST["contactMethod"] : "Email";
// If no errors, send email
if (empty($name_err) && empty($email_err) && empty($subject_err) && empty($message_err)) {
$to = "[email protected], [email protected]"; // IMCI's emails
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$email_body = "
New Contact Form Submission
Name: $name
Email: $email
Phone: " . ($phone ? $phone : "Not provided") . "
Subject: $subject
Preferred Contact Method: $contactMethod
Message:
" . nl2br($message) . "
";
if (mail($to, $subject, $email_body, $headers)) {
$success_msg = "Thank you! Your message has been sent. We will contact you soon.";
// Clear form fields
$name = $email = $phone = $subject = $message = $contactMethod = "";
} else {
$success_msg = "Oops! Something went wrong. Please try again later.";
}
}
}
?>
Contact Us
The information and views set out in this [report/study/article/publication/blog etc…] are those of the author(s) and do not necessarily reflect the official opinion of The Institute of Management Consultants of India. Neither the IMCI and bodies nor any person acting on their behalf may be held responsible for the use which may be made of the information contained therein.