<?php

  /**

   * Sets error header and json error message response.

   *

   * @param  String $messsage error message of response

   * @return void

   */

  function errorResponse ($messsage) {

    header('HTTP/1.1 500 Internal Server Error');

    die(json_encode(array('message' => $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 .= "<html><head>\r\n";

	$htmlhead .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n";

	$htmlhead .= "</head>\r\n";

	$htmlhead .= "<style type=\"text/css\">";

	$htmlhead .= "body

{

        margin: 0px;

        cursor: default;

        font-family: \"Segoe UI\", Sans-Serif;

        font-size: 12pt;

}

h1

{

        background-color: #ffc153;

        font-size: 14pt;

        font-weight: normal;

        padding: 3px 8px;

        margin: 0px;

}



#lastupdate

{

        padding: 3px 8px;

        color: #AAA;

}



#todolist

{

        background: #fff;

        width: 100%;

        border-collapse: collapse;

        text-align: left;

}

#todolist th

{

        font-weight: normal;

        background-color: #ffc153;

        padding: 4px 8px;

        border-bottom: 1px solid #F93;

        border-top: 1px solid #F93;

}

#todolist th:first-child

{

        padding-left: 25px;

}

#todolist td

{

        border-bottom: 1px solid #F93;

        padding: 3px 8px;

}

#todolist tbody tr.entryrow td:first-child

{

        padding-left: 25px;

}

#todolist tbody tr.entryrow:hover td

{

        background-color: #A7CDF0;

        cursor: pointer;

}

#todolist tbody tr.unseen td

{

        font-weight:bold;

}

#todolist tbody tr.folderheader td

{

        padding: 20px 8px 3px 8px;

        font-weight:bold;

}";

	$htmlhead .= " </style>\r\n";

	$htmlhead .= "<body>\r\n";

      

      

      

      

      $message_body =$htmlhead."<h1>New REQUEST / CONTACT</h1><br>

          Name: <strong>".$_POST['name']."</strong><br>\n".

          "Email: <strong>".$_POST['email']."</strong><br>\n".

          "Tel: <strong>".$_POST['phone']."</strong><br><br>\n".

          "Type of service: <strong>".$_POST['reason']."</strong><br><br>\n".

          "<u>Message: </u><br>\n".$_POST['message']."\n";

      

    $message_body .= "</body>\r\n";

	$message_body .= "</html>\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);

  }

?>

