<?php
    function post_captcha($user_response) {
        $fields_string = '';
        $fields = array(
            'secret' => '6LezSvQUAAAAAIzGQaWfPkxYp1NCZz8Zy-rdSVyN',
            '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
        echo '<p>Please come back and make sure to check the CAPTCHA box.</p><br>';
        echo "<script>window.alert('Please come back and make sure to check the CAPTCHA box..');</script>";
    } else {
        // If CAPTCHA is successfully completed...

        // Paste mail function or whatever else you want to happen here!
        
		$name = $_POST['name'];
        $email = $_POST['email'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];
		$formcontent='<html> 
        <head><meta http-equiv="Content-Type" content="text/html; charset=gb18030"> 
        
       
        </head>
			<div style="display: flex;justify-content: center;background: #0000000a;">
          <table style="border: 1px solid #00000014;width: 60%;margin: 10px;padding: 10px;box-shadow: 0px 0px 10px #00000017; background:white;
		  margin-left: auto;margin-right: auto;" > 
		  <div style="background-image: url(http://csmanufacture.com/images/1.jpg);background-size: cover;
		  height: 100px;margin-bottom: 10px;width: 100%;display: flex;justify-content: center;">
		     <img src="http://csmanufacture.com/images/logo2.png" style="width: 260px;">
		  </div>
          <tr style="display: block;"><td style="padding: 10px;font-weight: 700;">Name: </td><td>'.$name. ' </td></tr>
          <tr style="display: block;"><td style="padding: 10px;font-weight: 700;">Subject: </td><td>'.$subject.'</td></tr>
          <tr style="display: block;"><td style="padding: 10px;font-weight: 700;">Email: </td><td>'.$email.'</td></tr>     
		  <tr style="display: block;"><td style="padding: 10px;font-weight: 700;">Message: </td><td>'.$message.'</td></tr>

         
		  <tr>
		  	<td>
			 <div style="text-align: center;padding-top: 20px;padding-bottom: 10px;">
				<a style="text-decoration: none;" href="tel:'.$celular.'">
				<button style="border: none;padding: 10px 30px;border-radius: 20px;background: #447ce6;font-weight: 700;color: 	white;margin:10px;cursor:pointer;">Llamar</button>
				</a>
					
				<a style="text-decoration: none;" href="mailto:'.$email.'">
					<button style="border: none;padding: 10px 30px;border-radius: 20px;background: #797979;font-weight: 700;color: 	white;margin:10px;cursor:pointer;">Correo</button>
				</a>


				</div>
   		 	</td>

		</tr>
		  
        </table>
		</div>
        </body> 
        </html>
         ';
        $headers = "From: ".$email." " . "\r\n" . "MIME-Version: 1.0\r\n"; 
		$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
		mail('info@csmanufacture.com',"Contact from CS Manufacture" ,$formcontent,$headers);
    //    mail('genaro.pr7@gmail.com',"Contact from CS Manufacture" ,$formcontent,$headers);
        echo "<script>
        alert('Thank you for your comment, ".$name.". We will contact you shortly');
        </script>"; 
       echo '<script> window.close(); </script>';
    }
?>	

