<?php
require_once '../Control/ControlDB.php';
require_once '../Control/ControlSistema.php';
require_once '../Util/email/PHPMailerAutoload.php';



if ($_SERVER["REQUEST_METHOD"] == "POST")
	{

	// Error messages
	$name = $_POST['name'];
	$email = $_POST['email'];
	$phone = $_POST['phone'];
	$msgsubject = $_POST['subject'];
	$comments = $_POST['comments'];
	$verify = $_POST['verify'];
	if (trim($name) == '')
		{
		echo '<div class="alert alert-danger alert-dismissable">
  <button type="button" cllucianoass="close" data-dismiss="alert" aria-hidden="true">&times;</button>Atenção! Entre com seu nome.</div>';
		exit();
		}
	  else
	if (!filter_var($email, FILTER_VALIDATE_EMAIL))
		{
		echo '<div class="alert alert-danger alert-dismissable">
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>Atenção! Entre com um e-mail válido.</div>';
		exit();
		}
	  else
	if (trim($msgsubject) == '')
		{
		echo '<div class="alert alert-danger alert-dismissable">
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>Atenção! Selecione um setor.</div>';
		exit();
		}
	  else
	if (trim($comments) == '')
		{
		echo '<div class="alert alert-danger alert-dismissable">
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>Atenção! Entre com a mensagem.</div>';
		exit();
		}
	  else
	if (!isset($verify) || trim($verify) == '')
		{
		echo '<div class="alert alert-danger alert-dismissable">
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>Atenção! Entre com a respota do anti-spam.</div>';
		exit();
		}
	  else
	if (trim($verify) != '2')
		{
		echo '<div class="alert alert-danger alert-dismissable">
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>Atenção! A resposta da questão é somente: <b>2<b></div>';
		exit();
		}

	

	// Mail subject
	$subject = "Enviado pelo site por: $name";

	// Mail content
	$email_content = "Você foi contactado por $name Segue a mensagem: \"
$comments \" 
Você pode contactar $name via e-mail, $email";

	// Mail headers
	$email_headers = "From: $name <$email>";
        
        //Preprarar PHPMail
// Your e-mailadress.
	$recipient = ControlSistema::$cfgLocal->getEmail();
        $recipientPassword = ControlSistema::$cfgLocal->getSenha();
//istance        
$mail = new PHPMailer();        
$mail->CharSet = 'utf-8';
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = ControlSistema::$cfgLocal->gethost();
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = $recipient;
//Password to use for SMTP authentication
$mail->Password = $recipientPassword;
//Set who the message is to be sent from
$mail->setFrom($recipient, 'Enviado do Site San Karlo Hotel');
//Set an alternative reply-to address
$mail->addReplyTo($email, $name);
//Set who the message is to be sent to
$mail->addAddress($msgsubject, $msgsubject);
//Set the subject line
$mail->Subject = 'E-mail Enviando pelo Site San Karlo Hotel';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->MsgHTML($email_content);
//Replace the plain text body with one created manually
$mail->AltBody = $email_content;
//send the message, check for errors
if (!$mail->send()) {
    echo "<h1>Falha ao enviar e-mail!</h1>";
		echo '<div class="alert alert-error alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
		echo "<p>$mail->ErrorInfo</p>";
		echo '</div>';
    //echo "oops! Alguma coisa deu errado, vejas os detalhes: " . $mail->ErrorInfo;
} else {
    echo "<h1>E-mail enviado com sucesso!</h1>";
		echo '<div class="alert alert-success alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
		echo "<p>Obrigado <strong>$name</strong>, sua mensagem foi enviada.</p>";
		echo '</div>';
}
        
	}
  else
	{
	echo "<p>Houve um problema com o seu envio , por favor, tente novamente.</p>";
	}

?>