<?php
// configure
$from = 'Inquiry Mail <noreply@vidyaroadlinespackers.com';
$sendTo = "info@vidyaroadlinespackers.com";
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'mobile' => 'Phone', 'email' => 'Email', 'from' => 'Moving From', 'to' => 'Moving To', 'message' => 'Message');
// array variable name => Text to appear in email
$okMessage = 'Thank you, I will get back to you soon! <a href="index">Click here for Back to Home</a>';
$errorMessage = 'There was an error while submitting the form. Please try again later';

// let's do the sending

try {
    $emailText = "You have new message from contact form\n=============================\n";

    foreach ($_POST as $key => $value) {

        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    mail($sendTo, $subject, $emailText, "From: " . $from);

    $responseArray = array('type' => 'success', 'to' => $okMessage);
} catch (\Exception $e) {
    $responseArray = array('type' => 'danger', 'to' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
} else {
    echo $responseArray['to'];
}

?>

