$messsage)));
}
/**
* Pulls posted values for all fields in $fields_req array.
* If a required field does not have a value, an error response is given.
*/
function constructMessageBody () {
$fields_req = array("name" => true, "email" => true, "message" => true);
$message_body = "";
foreach ($fields_req as $name => $required) {
$postedValue = $_POST[$name];
if ($required && empty($postedValue)) {
errorResponse("$name is empty.");
}
// else {
// $message_body .= ucfirst($name) . ": " . $postedValue . "\n";
// }
}
$htmlhead .= "
\r\n";
$htmlhead .= "\r\n";
$htmlhead .= "\r\n";
$htmlhead .= "\r\n";
$htmlhead .= "\r\n";
$message_body =$htmlhead."New REQUEST / CONTACT
Name: ".$_POST['name']."
\n".
"Email: ".$_POST['email']."
\n".
"Tel: ".$_POST['phone']."
\n".
"Type of service: ".$_POST['reason']."
\n".
"Message:
\n".$_POST['message']."\n";
$message_body .= "\r\n";
$message_body .= "\r\n";
return $message_body;
}
header('Content-type: application/json');
//do Captcha check, make sure the submitter is not a robot:)...
$url = 'https://www.google.com/recaptcha/api/siteverify';
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query(array('secret' => '6LeT5goTAAAAAGWeVhyUJSoItToTNpUCIf8mwu4-', 'response' => $_POST["g-recaptcha-response"]))
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($url, false, $context, -1, 40000));
if (!$result->success) {
errorResponse('reCAPTCHA checked failed! Error codes: ' . join(', ', $result->{"error-codes"}));
}
//attempt to send email
$messageBody = constructMessageBody();
require './vender/php_mailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->IsHTML(true);
//$mail->isSMTP();
//$mail->Host = 'mail.maxer.hu';
//$mail->SMTPAuth = true;
//$mail->Username = 'mail@sys.co.hu';
//$mail->Password = 'titok123';
//$mail->SMTPSecure = '';
//$mail->Port = 587;
$mail->setFrom('kapcsolat@skyflytravel.hu');
$mail->addAddress('info@skyflytravel.hu');
$mail->Subject = 'New request, '.$_POST['reason'];
$mail->Body = $messageBody;
//try to send the message
if($mail->send()) {
echo json_encode(array('message' => 'Your message was successfully submitted. Our colleague will contact you soon.'));
} else {
errorResponse('An expected error occured while attempting to send the email: ' . $mail->ErrorInfo);
}
?>