<?php
include("mail/PHPMailer.php");
include('mailer/src/SMTP.php');
function pool_mail()
{
  $array = array();
  $array[] = array(
    'username' => 'sistemaclaves@urp.edu.pe',
    'password' => '515t3m4cl4v3172o9'
  )
  ;
  $array[] = array(
    'username' => 'sistemaclaves1@urp.edu.pe',
    'password' => '515t3m4cl4v3172o9'
  )
  ;
  $array[] = array(
    'username' => 'sistemaclaves2@urp.edu.pe',
    'password' => '515t3m4cl4v3172o9'
  )
  ;
  $array[] = array(
    'username' => 'sistemaclaves3@urp.edu.pe',
    'password' => '515t3m4cl4v3172o9'
  )
  ;
  $array[] = array(
    'username' => 'sistemaclaves4@urp.edu.pe',
    'password' => '515t3m4cl4v3172o9'
  )
  ;
  return $array;
}
function isEmpty($value)
{
  if (empty($value)) {
    $return = true;
  } else {
    $return = false;
  }
  return $return;
}
function sendMail($credencial, $fromName, $correo, $asunto, $mensaje, $attachment = null)
{
  $mail = new PHPMailer();
  $mail->IsSMTP();
  $mail->SMTPAuth = true;
  $mail->SMTPDebug = 0; /*Detalle*/
  //if ($_SERVER['REMOTE_ADDR'] == '10.130.29.241') {
  //$mail->SMTPDebug  = 3;//f::message($credencial); 
  //}
  $mail->CharSet = 'UTF-8';
  $mail->Port = 587;
  $mail->SMTPSecure = "tls";
  $mail->Host = "smtp.gmail.com";
  $mail->Username = $credencial['username'];
  $mail->Password = $credencial['password'];


  $mail->From = $mail->Username;
  $mail->SetFrom($mail->From, $fromName);
  //$mail->addBCC("epalomino@urp.edu.pe","COPIA"); 
  $mail->FromName = $fromName;

  $array_correo = explode(',', $correo);
  foreach ($array_correo as $value) {
    $value = trim($value);
    if (!isEmpty($value)) {
      $mail->AddAddress($value);
    }
  }
  if (!isEmpty($attachment)) {
    $array_attachment = explode(',', $attachment);
    foreach ($array_attachment as $value) {
      $value = trim($value);
      if (!isEmpty($value)) {
        $mail->AddAttachment($value);
      }
    }
  }
  message('----------');
  message($asunto);
  message('----------');
  $mail->WordWrap = 0; // set word wrap
  $mail->IsHTML(true); // send as HTML
  $mail->Subject = $asunto;
  $mail->Body = $mensaje; //HTML Body
  $mail->AltBody = strip_tags($mail->Body); // Este es el contenido alternativo sin html

  return $mail->Send();
}
function message($contenido, $clase = null, $echo = true)
{
  if (isEmpty(trim($clase))) {
    $clase = 'ui-state-highlight';
  }

  if (is_object($contenido) or is_array($contenido)) {
    $return = print_r($contenido, true);
  } else {
    $return = $contenido;
  }

  $return = '
            <div class="' . $clase . ' ui-corner-all" style="padding:5px;margin:2px 0px;">
                <pre style="padding:0px;margin:0px;">' . $return . '</pre>
            </div>
        ';

  if ($echo) {
    echo $return;
  } else {
    return $return;
  }
}

function eviarMail($fromName, $correo, $asunto, $mensaje)
{
  $pool_mail = pool_mail();
  shuffle($pool_mail); //de manera aleatoria reordena los items
  foreach ($pool_mail as $credencial) {
    if (sendMail($credencial, $fromName, $correo, $asunto, $mensaje)) {
      break;
    }
  }
}

$datos = $_POST['datos'];
$datos = base64_decode($datos);
$datos = json_decode($datos, true);
eviarMail($datos['fromName'], $datos['correo'], $datos['asunto'], base64_decode($datos['mensaje']));
message($datos);
message(base64_decode($datos['mensaje']));