<?php
if (isset($_POST['form_name'])) {

    $name    = trim($_POST['form_name']);
    $email   = trim($_POST['form_email']);
    $phone   = trim($_POST['form_phone']);
    $subject = trim($_POST['form_subject']);
    $message = trim($_POST['form_message']);

    if ($name == '' || $email == '' || $phone == '' || $subject == '' || $message == '') {
        echo '<div id="error">All fields are required.</div>';

    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo '<div id="error">Please enter a valid email address.</div>';

    } else {

        $to = "info@akemilex.com"; // change if needed
        $mail_subject = "New Contact Form Enquiry - $subject";

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

        $headers  = "From: Website <no-reply@akemilex.com>\r\n";
        $headers .= "Reply-To: $email\r\n";

        if (mail($to, $mail_subject, $mail_message, $headers)) {
            echo '<div id="success">Thank you! Your message has been sent successfully.</div>';
        } else {
            echo '<div id="error">Mail server error. Please try again later.</div>';
        }
    }
}
?>
