<?php

if($_SERVER['REQUEST_METHOD'] != 'POST') {
	include error(403);
	die;
}

if($_POST['captcha'] != '') {
	include error(403);
	die;
}

$senderName = trim(filter_var($_POST['name'], FILTER_SANITIZE_STRING));
$senderEmail = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

$headers  = '';
$headers .= "Content-Type: text/plain; charset=utf-8\r\n";
$headers .= "From: $senderName <$senderEmail>\r\n";

$result = mail("andrea.franceschini@gmail.com", "Website contact", $message, $headers);

if($result == 1) {
	$ref = filter_var($_SERVER['HTTP_REFERER'], FILTER_SANITIZE_URL);
	session_start();
	$_SESSION['contact_thanks'] = 1;
	header("Location: $ref");
}