<?php
echo "<pre>"; print_r($_POST)  ;  echo "</pre>".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; 
//error_reporting(0);
//$sender_name = stripslashes($_POST["sender_name"]);
	$sender_email = stripslashes($_POST["email"]);
	$sender_message = stripslashes($_POST["form_result"]);

if (isset($_POST['g-recaptcha-response'])) {
    $captcha = $_POST['g-recaptcha-response'];
    $privatekey = "6LdQ03IUAAAAAEcHLHukEBHML-g8FFy6zqohXNZQ";
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = array(
        'secret' => $privatekey,
        'response' => $captcha,
        'remoteip' => $_SERVER['REMOTE_ADDR']
    );

    $curlConfig = array(
        CURLOPT_URL => $url,
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => $data
    );

    $ch = curl_init();
    curl_setopt_array($ch, $curlConfig);
    $response = curl_exec($ch);
    curl_close($ch);
}

$jsonResponse = json_decode($response);

if ($jsonResponse->success === true) {
  		require_once "PMail.php";

			$from = '<casstomdev@gmail.com>';
			$to = "<".$sender_email.">";
			$subject = 'Wynik testu:'.$sender_message;
			$body = '(Wiadomość testowa) Wynik testu:\n '.$sender_message;

			$headers = array(
				'From' => $from,
				'To' => $to,
				'Subject' => $subject
			);

			$smtp = Mail::factory('smtp', array(
					'host' => 'ssl://smtp.gmail.com',
					'port' => '465',
					'auth' => true,
					'username' => 'casstomdev@gmail.com',
					'password' => '11KasiaTomek06'
				));

			$mail = $smtp->send($to, $headers, $body);

			if (PEAR::isError($mail)) {
				echo('<p>' . $mail->getMessage() . '</p>');
			} else {
				echo('<p>Message successfully sent!</p>');
			} 
}
else {
  	echo('<p>failed</p>');
}














	 
	if ($captcha_success->success==false) {
		$loc= "Location: ".$_SERVER['HTTP_HOST']."/#/autyzm";
		//$uri = "Location: ".$_SERVER['HTTP_HOST']."/autyzm";
		//header("Location: http://".$_SERVER['HTTP_HOST']."/autyzm", true, 302);
		echo $verify."ddd".json_encode($captcha_success)."ddd";
		//die();
	} else if ($captcha_success->success==true) {  

			// Pear Mail Library
		
		/*$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/plain;charset=UTF-8" . "\r\n";
		$message = 'Wynik testu</br> '.$sender_message;
		$headers .=  'From: <casstomdev@gmail.com>' . "\r\n" .
			'Reply-To: casstomdev@gmail.com' . "\r\n" ;

		mail($to, $subject, $message, $headers); 
		*/
		//header("Location: http://".$_SERVER['HTTP_HOST']."/#/autyzm/sendresult", true, 302);
		//die();
	}