<?php
class Email {
  function __construct() {
    // require_once '../PHPMailer/PHPMailerAutoload.php'; // Carga la clase de envío
  }
  function Prepara_Texto($fichero, $param, $valores) {
    $Salida = "";
    $f = fopen($fichero, 'r');
    while(!feof($f)) {
      $Salida .= fgets($f);
    }
    fclose($f);  
    foreach ($param as $campo => $v) {
      if (isset($valores[$campo])){$Salida = str_replace($v, $valores[$campo], $Salida);}
    }
    $this->texto = $Salida;
  } 
  function Enviar_Email($mail, $cfg, $destino, $copias, $ocultas, $asunto, $body, $adj1, $adj2) {  

    if (is_array($adj1)){
      if (!isset($adj1[0])){$adjunto1[0] = $adj1;} else {$adjunto1[0] = $adj1[0];}
      if (!isset($adj1[1])){$adjunto1[1] = "";} else {$adjunto1[1] = $adj1[1];}
    }
    else {
      $adjunto1[0] = "";
      $adjunto1[1] = "";
    }
    if (is_array($adj2)){
      if (!isset($adj2[0])){$adjunto2[0] = $adj2;} else {$adjunto2[0] = $adj2[0];}
      if (!isset($adj2[0])){$adjunto2[1] = "";} else {$adjunto2[1] = $adj2[1];}
    }
    else {
      $adjunto2[0] = "";
      $adjunto2[1] = "";
    }
    
    // Envio del correo
    try {
      //Server settings
      $mail->isSMTP();                            // Send using SMTP
      $mail->CharSet    = 'utf-8'; 
      $mail->SMTPDebug  = 0;                      // Enable verbose debug output
      $mail->Host       = $cfg['email_host'];     // Set the SMTP server to send through
      $mail->Port       = $cfg['email_port'];     // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
      $mail->SMTPSecure = $cfg['email_secure'];   // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
      $mail->SMTPAuth   = true;                   // Enable SMTP authentication
      $mail->Username   = $cfg['email_username']; // SMTP username
      $mail->Password   = $cfg['email_passmail']; // SMTP password

      $mail->setFrom($cfg['email_from'], $cfg['email_fromName']);
      $mail->addAddress($destino[0], $destino[1]);     // Add a recipient
      $mail->addReplyTo($cfg['email_from'], $cfg['email_fromName']);
      if (is_array($copias)){
        for ($i=0; $i<count($copias); $i++){
          $mail->addCC($copias[$i]);
        }
      }
      elseif($copias != ""){$mail->addCC($copias);}
      if (is_array($ocultas)){
        for ($i=0; $i<count($ocultas); $i++){
          $mail->addBCC($ocultas[$i]);
        }
      }
      $mail->isHTML(true);                                  // Set email format to HTML
      $mail->Subject = $asunto;
      $mail->Body    = $body;
      $mail->AltBody = $body;
      // Attachments
      if ($adjunto1[0] != ""){$mail->addAttachment($adjunto1[0], $adjunto1[1]);}
      if ($adjunto2[0] != ""){$mail->addAttachment($adjunto2[0], $adjunto2[1]);}
      $mail->send();
      $this->error = ""; 
    }
    catch (Exception $e) {$this->error = "Error: ".$mail->ErrorInfo;}  
    // Fin envio
  } 
}
        
//      if(!PHPMailer::validateAddress($$destino)) {throw new phpmailerAppException("Dirección de email: " . $destino[0] . " invalida -- abortado!");}
//      $mail->SMTPOptions = array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true));
//      $mail->WordWrap = 80; 
//      $mail->msgHTML($body, dirname(__FILE__), true); //Create message bodies and embed images 
