<?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"]).stripslashes($_POST["form_result_description"]);

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 'PHPMailer.php';
			require 'SMTP.php';
			require 'Exception.php';
		  
	//TODO: nazwa narzędzia
		$subject = 'Wynik badania z wykorzystaniem testu: ';
			$body = 'Twój wynik to:\n '.$sender_message; 
			$body = '(Wiadomość testowa) Wynik testu:\n '.$sender_message; 
		 
 
	 
		 $mail = new PHPMailer\PHPMailer\PHPMailer();
			 $mail->IsSMTP();
			 $mail->SMTPAuth = true; 
			 $mail->Mailer = 'smtp';
			 $mail->SMTPSecure = 'ssl'; 
			 $mail->Host = 'smtp.gmail.com';
			 $mail->Port = 465;   
		 // $mail->SMTPDebug = 2;
			 $mail->Username = 'casstomdev@gmail.com';  
			 $mail->Password = '11KasiaTomek06';
			      
			 $mail->SetFrom('casstomdev@gmail.com', 'autyzm');
			 $mail->Subject = $subject;
			 $mail->Body = $body;
			 $mail->AddAddress($sender_email);
			 echo('<p>test</p>');
			 if(!$mail->Send()) {
			 $error = 'Mail error: '.$mail->ErrorInfo;
			  echo( $error);
			 return false;
			 } else {
			 $error = 'Message sent!';
			  echo( $error);
			  header("Location: http://".$_SERVER['HTTP_HOST']."/#/sendresult", true, 302);
		 die();
			 return true;
			 }
}
else {
  	echo('<p>failed</p>');
}
header("Location: http://".$_SERVER['HTTP_HOST']."/autyzm", true, 302);

die();











	 
	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();
	}