<?php

  // $Enviar = 'Ok';

  if( $_POST['enviar'] == 'Ok' || $Enviar == 'Ok' ){

    $Position = '';
    require_once($Position.'config/config.php');
    require_once($Position.'config/conexion.php');

    //Config Correo
    $Query = "
      SELECT
        con.Id, con.FromEmail, con.FromName, con.Replayto,
        con.Host, con.Port, con.User, con.Pass,
        con.SMTPDebug, con.SMTPAuth, con.SMTPSecure,
        con.verify_peer, con.verify_peer_name, con.allow_self_signed
      FROM config_envio_correos con
      WHERE con.Id = '2'
    ";
    $Result = mysqli_query($mysqli, $Query);
    $ConfigSender = mysqli_fetch_assoc($Result);

    $Id			      = $_POST['id'];
    $Titulo			  = $_POST['titulo'];
    $Para 			  = $_POST['mail'];
    $DeNombre     = $_POST['DeNombre'];
    //$Copia 			= $_POST['Copia'];
    $Mensaje		  = $_POST['mensaje'];

    //$DeNombre = 'Kontakt';
    // $Titulo   = 'Mensaje desde SwitchPoint';
    // $Para     = 'fernando.ramirez.garcia@gmail.com';
    // $Mensaje  = 'Envio desde Kontakt correo de salida.';

    $subject = $Titulo;

    //$message = 'New -';
    $message = $Mensaje;

    if( $DeNombre ){
      $from_name = $DeNombre;
    }else{
      $from_name = $ConfigSender[FromName];
    }
    $from_mail = $ConfigSender[FromEmail];
    $replyto   = $ConfigSender[Replayto];
    //$mailto		 = "fernando.ramirez.garcia@gmail.com";
    $mailto		 = $Para;

    require($RootServer."/PHPMailer-master/src/PHPMailer.php");
    require($RootServer."/PHPMailer-master/src/Exception.php");
    require($RootServer."/PHPMailer-master/src/SMTP.php");

    $mail = new PHPMailer\PHPMailer\PHPMailer();
    $mail->IsSMTP(); // enable SMTP

    $mail->SMTPDebug    = $ConfigSender[SMTPDebug]; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth     = $ConfigSender[SMTPAuth]; // authentication enabled
    $mail->SMTPSecure   = $ConfigSender[SMTPSecure]; // secure transfer enabled REQUIRED for Gmail
    $mail->SMTPOptions  = array(
      'ssl' => array(
        'verify_peer' => $ConfigSender[verify_peer],
        'verify_peer_name' => $ConfigSender[verify_peer_name],
        'allow_self_signed' => $ConfigSender[allow_self_signed]
      )
    );
    $mail->Host = $ConfigSender[Host];
    $mail->Port = $ConfigSender[Port]; // or 587 , 465, 25
    $mail->IsHTML(true);
    $mail->CharSet = 'UTF-8';
    $mail->Username = $ConfigSender[User];
    $mail->Password = $ConfigSender[Pass];
    $mail->SetFrom($ConfigSender[FromEmail], $from_name);
    $mail->IsHTML(true);
    $mail->Subject = $subject;
    $mail->Body = $message;

    //File Attach
    // $file     = $RootServer."/api/absender/pdf/files/Ticket-".$Id.".pdf";
    // $filename = "Ticket-".$Id.".pdf";
    // $mail->AddAttachment($file,$filename);

    $mail->AddAddress($mailto);
    //$mail->addCC('copiado@hotmail.com');

    if(!$mail->Send()) {
      //echo "Mailer Error: " . $mail->ErrorInfo;
      //echo 'Enviado';
      echo $MailSended = 'Error';

    }else{
      //echo "Message has been sent";
      //echo "Error";
      //echo 'Enviado';
      echo $MailSended = 'Ok';

    }
    //End If Insert Send Email

  }

?>
