<?php
require_once '../Control/ControlDB.php';
require_once '../Control/ControlSistema.php';
require_once '../Util/email/PHPMailerAutoload.php';
require_once '../Control/ControlContato.php';
$controlContato = new ControlContato;
$contato = $controlContato->pegarUmContato(1);


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

	// Error messages
	$email = $_POST['newsletter'];
	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();
		}
	  

	

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

	// Mail content
	$email_content = "O $email foi adicionado para receber novidades e promoções do San Karlo Hotel";

	// 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 = 25;
//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, 'Assinatura de Newsletter realizado no Site San Karlo Hotel');
//Set an alternative reply-to address
$mail->addReplyTo($email, $email);
//Set who the message is to be sent to
$mail->addAddress($contato->getEmail(), $contato->getEmail());
//Set the subject line
$mail->Subject = 'Assinatura de Newsletter realizado no 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 requisição de assinatura de Newsletter!</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>';
} else {
    //echo "<h1>Assinatura realizado 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>$email</strong>, sua assinatura foi realizada.</p>";
		echo '</div>';
}
        
	}
  else
	{
	echo "<p>Houve um problema com o seu envio , por favor, tente novamente.</p>";
	}

?>