<?php
class Pampa_Util_Mail {

  var $html_file; 	//ARQUIVO HTML
  var $html_data; 	//INFORMCOES DO HTML
  var $mail_to;		//DESTINATARIO DO EMAIL
  var $mail_cco;		//DESTINATARIO DO EMAIL OCULTO
  var $mail_from;	//REMETENTE DO EMAIL
  var $mail_subject;	//ASSUNTO DO E-MAIL
  var $mail_body;	//TEXTO (CORPO) DO E-MAIL

  var $file_open;		//ARQUIVO ABERTO OU NAO
  var $data_set;		//VARIAVEIS TRATADAS
  var $utf_8;			//SE A CODIFICAÇÃO É UTF-8
  var $isFile = false; //SE ESCREVE EM ARQUIVO
  var $mailDir; //DIRETÓRIO DE ARMAZENAMENTO


  public function Pampa_Util_Mail() {
    //VERIFICA SE ESTA HABILITADO
    if(ini_get("allow_url_fopen") != 1) {
      ini_set("allow_url_fopen", 1);
    }

    //INICIA AS VARIAVEIS COMO FALSE
    $this->file_open = false;
    $this->data_set = false;
    $this->utf_8 = false;
    $this->mailDir = APPLICATION_PATH."/views/emails/";
    // valida o modulo
    $frontController = Zend_Controller_Front::getInstance();
    if($frontController->getRequest()->getModuleName() != "default"){
      $this->mailDir = $frontController->getModuleDirectory()."/views/emails/";
    }

    /**/
    $config = array (
            'auth' => 'login',
            'username' => 'chao@londigital.com.br',
            'password' => 'Q!w2E#r4',
            'ssl' => 'tls',
            'port' => '587'
    );
    // seta o transport padrao
    $mailTransport = new Zend_Mail_Transport_Smtp("smtp.gmail.com", $config);
    Zend_Mail::setDefaultTransport($mailTransport);
    /* */
  }

  function loadHTML($file)//CARREGA O ARQUIVO HTML
  {
    $this->html_file = file_get_contents($this->mailDir.$file);	//ABRE O ARQUIVO
    if($this->html_file != false){
      $this->html_file = utf8_decode($this->html_file);
    }
    $this->file_open = ($this->html_file != false);	//VALIDA SE O ARQUIVO FOI ABERTO
  }

  function setData($data, $utf = false)//RECEBE OS DADOS QUE SERAO INCLUSOS NO HTML
  {
    $this->html_data = $data;
    $this->utf_8 = $utf;

    if($this->file_open) {
      $this->createData();
    }
  }

  //ATRIBUI AS VARIAVEIS AO TEXTO
  function createData() {
    if($this->file_open && is_array($this->html_data)) {
      $a = $this->html_data; //VARIAVEL CURTA
      $this->mail_body = $this->html_file; //ATRIBUI A VARIAVEL A STRING PURA

      foreach($a as $key => $value) {//VARRE A STRING E SUBSTITUI AS VARIAVEIS
        $this->mail_body = str_replace("#{".strtoupper($key)."}", utf8_decode($value), $this->mail_body);
      }

      $this->data_set = true;
    }
  }

  function setHeader($to, $from, $subject, $cco = null)//ADICIONA AS INFORMACOES DE CABECALHO
  {
    $this->mail_to = $to;
    $this->mail_from = $from;
    $this->mail_subject = $subject;
    $this->mail_cco = $cco;
  }

  function send()//ENVIA O E-MAIL
  {
    if($this->data_set) {
      // cria o objeto zendMail
      $zendMail = new Zend_Mail();
      $zendMail->setFrom($this->mail_from, $this->mail_from);
      $zendMail->setReplyTo($this->mail_from);
      $zendMail->setReturnPath($this->mail_from);

      if(!is_null($this->mail_cco)){
        $zendMail->addBcc($this->mail_cco);
      }

      try {
        //VALIDA SE ENVIA OU ESCREVE ARQUIVO
        $zendMail->addTo($this->mail_to);
        $zendMail->setBodyHtml($this->mail_body);
        $zendMail->setSubject(utf8_decode($this->mail_subject));
        if(!$this->isFile) {
          return $zendMail->send();
        } else {
          $file_name = $this->mailDir.$this->mail_subject."_".time().".htm";
          while(file_exists($file_name)){
            $file_name = $this->mailDir.$this->mail_subject."_".time().".htm";
          }
          $f = fopen($file_name, "w");
          if(fwrite($f, $this->mail_body)) {
            fclose($f);
            return true;
          } else {
            return false;
          }
        }
      } catch(Zend_Mail_Exception $e) {
        Pampa_Util_Dump::text($e->getMessage());
      } catch(Exception $e) {
        Pampa_Util_Dump::text($e->getMessage());
      } catch(Error $e) {
        Pampa_Util_Dump::text($e->getMessage());
      }
    }
  }

}