<?php
    if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $phone = $_POST['phone'];
    $property = $_POST['property'];
    $askprice = $_POST['askprice'];
    $details = $_POST['details'];
    $reason = $_POST['reason'];
   

    $to = "info@luvanexgroup.com";
    $email_form = 'sellyourhouse@luvanexgroup.com';
    $email_subject = "Luvanex Holdings Form";
    $email_body = "User Name: $name.\n".
                  "user Email: $visitor_email.\n".
                  "Phone: $phone.\n".
                  "Address of property: $property. \n". 
                  "Ask Price: $askprice. \n".
                  "Additional Details: $details. \n".
                  "Reason for selling: $reason. \n" ;


    
    $headers = "From: $email_form \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";

    
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    function post_captcha($user_response) {
        $fields_string = '';
        $fields = array(
            'secret' => '6LcNlegUAAAAAGKkIqihFlnl6-iyeyi3g0EAs7dj
            ',
            'response' => $user_response
        );
        foreach($fields as $key=>$value)
        $fields_string .= $key . '=' . $value . '&';
        $fields_string = rtrim($fields_string, '&');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

        $result = curl_exec($ch);
        curl_close($ch);

        return json_decode($result, true);
    }
        
    // Call the function post_captcha
    $res = post_captcha($_POST['g-recaptcha-response']);

    if ($res['success']) {
    // What happens when the CAPTCHA wasn't checked            
     
        mail($to,$email_subject,$email_body,$headers);

        $to      = $visitor_email;
        $email_form = 'info@luvanexgroup.com';
        $subject = 'Thanks for Contacting Us';
        $message = "Hello $name \n" .
                    "Thanks for filling our form, we will contact you within one or two working days." ;

        $headers = "Reply-To: $visitor_email \r\n";
        
        mail($to, $subject, $message, $headers);
            {
                header("Location: Thanks.html");
            } 
    } else {
    // If CAPTCHA is successfully completed...

    // Paste mail function or whatever else you want to happen here!

    echo "<script>
    alert('Invalid Captcha, please try again');
    window.location.href='contact.html';
    </script>";
    }
}
}

?>