<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
	<title>E-mail</title>
	<meta charset="utf-8">
</head>
<body>
<form name="form1" method="post">
	<input type="email" name="email" placeholder="Digite o seu e-mail" required size="40">
	<br><br>
	<select name="servidor" required>
		<option value="">Selecionar servidor</option>
		<option value="1">Serainfo</option>
		<option value="2">Redenld</option>
	</select>
	<br><br>
	<button type="submit">Enviar</button>
</form>


<?php
	ini_set('display_errors',1);
	ini_set('display_startup_erros',1);
	error_reporting(E_ALL);

	if ( $_POST ) {

		$email = $servidor = "";

		if ( isset ( $_POST["email"] ) ) $email = trim ( $_POST["email"] );
		if ( isset ( $_POST["servidor"] ) ) $servidor = trim ( $_POST["servidor"] );

		if ( empty ( $servidor ) ) {
			echo "<div class='alert alert-danger'>Selecione o servidor</div>";
		} else if ( !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
			echo "<div class='alert alert-danger'>Preencha o campo e-mail com um endereço válido</div>";
		} else {
	
			if ( !isset ( $_SESSION["emails"] ) ) $_SESSION["emails"] = 1;
			else $_SESSION["emails"]++;

			if ( $_SESSION["emails"] > 15 ) {
				echo "<p>Já foram enviados 5 e-mails</p>";
				exit;
			}

			require 'PHPMailerAutoload.php';

			$mail = new PHPMailer;
			$mail->isSMTP();
			$mail->SMTPDebug = 1;
			$mail->Debugoutput = 'html';
			$mail->SMTPAuth   = true;  
		    //$mail->SMTPSecure = "ssl"; 

		    if ( $servidor == "1" ) {
		    	$mail->Host       = "mta01.redendl.com.br";
		    	//$mail->Host 		="srv-mta-nls-01.r4.net.br"; 
			    $mail->Port       = 587;                    
			    $mail->Username   = "boletos@serainfo.com.br"; 
			    $mail->Password   = "31S0aPFZ";
			    $servidor =" Serainfo";
		    } else {
			    //$mail->Host       = "mta01.serainfo.com.br"; 
			    $mail->Host 		="srv-mta-nls-01.r4.net.br";
			    $mail->Port       = 587;                    
			    $mail->Username   = "boletos@redendl.com.br"; 
			    $mail->Password   = "rbk@2017!";
			    $servidor =" Rede";
			}

		    $mail->setFrom($email, "Teste de Boletos");
			$mail->addAddress("burnes@professorburnes.com", "Burnes");
			$mail->addAddress("financeiro01@serainfo.com.br", "Thiago");
			$mail->addAddress($email, "Teste RBK");

			$mensagem = utf8_decode("<p>Este é só um teste de envio de e-mail!</p>");

			$mail->Subject = "Teste de Envio - $servidor";
			$mail->msgHTML("$mensagem");

			if (!$mail->send()) {
			    echo "<p>Erro ao enviar mensagem : " . $mail->ErrorInfo. " ". $_SESSION["emails"]."</p>";
			} else {
			    echo  "<p>E-mail enviado ".$_SESSION["emails"]."</p>";               	
			}
		}
	}
?>
</body>
</html>