<?php
    require_once('variables.php');

    $txtName = $_POST["txtName"];
    $txtEmail = $_POST["txtEmail"];
    $txtRating = "";
    $txtWebsite = "";
    $txtMessage = $_POST["txtMessage"];
    
    if ($formType == "comments") {
        $txtWebsite = $_POST["txtWebsite"];
    }
    
    if ($formType == "reviews") {
        $txtRating = $_POST["txtRating"];
    }
    
    $success = TRUE;    
    
    try{
        if(trim($txtEmail)==NULL){
            throw new Exception("Please enter your e-mail id.");
        }
        else
        {
            if (!preg_match("/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", trim($txtEmail)))
            {
                throw new Exception("Please enter the valid e-mail.");
            }
        }

        if(trim($txtName)==NULL){
            throw new Exception("Please enter your name.");
        }
    
        if(trim($txtMessage)==NULL){
            throw new Exception("Please enter your message.");
        }
    
        $MailSubject = "New Contact Mail";
        $MailBody = "<strong>Email: </strong> ".$txtEmail."<br />";
        $MailBody .= "<strong>Name: </strong> ".$txtName."<br />";
        $MailBody .= "<strong>Message: </strong> ".$txtMessage."<br />";
    
        $MailFrom = $txtEmail;
    
        $MailHeaders = 'MIME-Version: 1.0' . "\r\n";
        $MailHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    
        $MailHeaders .= "From: <".$txtEmail.">"."\r\n";
    
    
        $result =  mail($MailTo, $MailSubject, $MailBody, $MailHeaders);
    
        if(!$result){
            throw new Exception();
        }
    
        $success = TRUE;
    }
    catch (Exception $e){
        $success = FALSE;
    }
    
    if ($success)
    {
        // add your custom success message here
		    echo json_encode(
			    array(
				    'status' => 'success',
				    'message' => 'Mail sent successfully :)'
			    )
		    );        
    }
    else
    {
		    echo json_encode(
			    array(
				    'status' => 'error',
				    'message' => 'Mail was not sent :('
			    )
		    );            
    }
?>