<?php

function send_mail($email, $subject, $msg, $from) {

	$config = array();
	$config['api_key'] = "key-66621uwx8ylr5ezm21f-3gixzzono1n4";
 	$config['api_url'] = "https://api.mailgun.net/v3/oficinaitapeti.com.br/messages";

	$message = array();
	$message['from'] = "Oficina Itapeti <contato@oficinaitapeti.com.br>";
	$message['to'] = $email;
	$message['h:Reply-To'] = $from;
	$message['subject'] = $subject;
	$message['html'] = $msg;

	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $config['api_url']);
	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
	curl_setopt($ch, CURLOPT_USERPWD, "api:{$config['api_key']}");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $message);

	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
}

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);

echo send_mail($request->to, $request->subject, $request->msg, $request->from);

?>
