<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);


/*
	Creado por Engelberth
	Script que gestiona el envío de un formulario por correo electrónico a la cuenta indicada.
*/

//Correo de destino; donde se enviará el correo.
$correoDestino = "mercadeo@insepet.com";

//Texto emisor; sólo lo leerá quien reciba el contenido.
$textoEmisor = "MIME-VERSION: 1.0\r\n";
$textoEmisor .= "Content-type: text/html; charset=UTF-8\r\n";
$textoEmisor .= "From: Formulario creado desde Insepet.com";

/*
	Recopilo los datos vía POST
	Con strip_tags suprimo etiquetas HTML y php para evitar una posible inyección.
	Como no gestiona base de datos no es necesario limpiar de inyección SQL.
*/
$nombre = strip_tags($_POST['Name']);
$correo = strip_tags($_POST['Email']);
$telefono = strip_tags($_POST['Phone']);
$pais = strip_tags($_POST['Country']);
$solicitud = strip_tags($_POST['Solicitud']);
$comentario = strip_tags($_POST['Message']);


//Formateo el asunto del correo
$asunto = "Contacto WEB - $nombre";

//Formateo el cuerpo del correo
$cuerpo = "<b>Nombre:</b> " . $nombre . "<br />";
$cuerpo .= "<b>E-mail:</b> " . $correo . "<br />";
$cuerpo .= "<b>Teléfono: </b>" . $telefono . "<br />";
$cuerpo .= "<b>País: </b>" . $pais . "<br />";
$cuerpo .= "<b>Tipo de Solicitud: </b>" . $solicitud . "<br />";
$cuerpo .= "<b>Mensaje:</b> " . $comentario;


try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'mail.insepet.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'grupoempresarial@insepet.com';                     //SMTP username
    $mail->Password   = 'Cand4d0#5912';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;            //Enable implicit TLS encryption
    $mail->Port       = 587;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('grupoempresarial@insepet.com', 'grupoempresarial');
    $mail->addAddress($correoDestino);		            //Name is optional

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = $asunto;
    $mail->Body    = $cuerpo;
    $mail->addCustomHeader('Content-type: text/html; charset=UTF-8');

    $mail->send();

    header("location:nosotros/gracias.html");
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

// Envío el mensaje
//mail( $correoDestino, $asunto, $cuerpo, $textoEmisor);

?>