<?php
header("Access-Control-Allow-Origin: *");
$rest_json = file_get_contents("php://input");
$_POST = json_decode($rest_json, true);

if (empty($_POST['name']) && empty($_POST['email'])) die();

if ($_POST)
	{

	// set response code - 200 OK

	http_response_code(200);
	$subject = "Contact form message from " . $_POST['name'];
	$to = "info@naplozz.hu";
	$from = "contact@naplozz.hu";

	// data
  
  // $body = str_replace("\n","<br>",$_POST['message']);
  
  date_default_timezone_set('Europe/Budapest');
  $date = date('Y-m-d H:i:s');

  $msg = "Name: " . $_POST['name'] . "<br><br>Email: " . $_POST['email'] . "<br><br>Tel: " . $_POST['phone'];
  
	// Headers

	$headers = "MIME-Version: 1.0\r\n";
	$headers.= "Content-type: text/html; charset=UTF-8\r\n";
	$headers.= "From: <" . $from . ">";
	$sent = mail($to, $subject, $msg, $headers);

    $errorMessage = "";
    
	if (!$sent) {
        $errorMessage = error_get_last()['message'];
    }

	echo json_encode(array(
		"sent" => $sent,
		"message" => $msg,
		"error" => $errorMessage
	));
	}
  else
	{

	// tell the user about error
	
	$errorMessage = error_get_last()['message'];

	echo json_encode([
	    "sent" => false,
	    "message" => "Something went wrong",
	    "error" => $errormessage
	]);
	}

?>
