<?php
// Display errors if something is misconfigured on the server
ini_set('display_errors', 1);
error_reporting(E_ALL);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get form fields and remove whitespace
    $name = strip_tags(trim($_POST["name"]));
    $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $phone = strip_tags(trim($_POST["phone"]));
    $message = trim($_POST["message"]);

    // Check that data was sent
    if ( empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        header("Location: contact.php?success=-1");
        exit;
    }

    // Recipient email
    $recipient = "info@3mmarketing.pk";
    $subject = "New Website Contact from: $name";

    // Email content
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n";
    $email_content .= "Phone: $phone\n\n";
    $email_content .= "Message:\n$message\n";

    // Email headers MUST match the domain on cPanel
    $email_headers = "From: info@3mmarketing.pk\r\n";
    $email_headers .= "Reply-To: $email\r\n";
    $email_headers .= "Content-Type: text/plain; charset=UTF-8\r\n";

    // Send the email with @ to suppress fatal config errors from cPanel
    if (@mail($recipient, $subject, $email_content, $email_headers)) {
        header("Location: contact.php?success=1");
        exit;
    } else {
        header("Location: contact.php?success=-1");
        exit;
    }
} else {
    header("Location: contact.php");
    exit;
}
?>
