<?php
require_once("include/phpmailer/class.phpmailer.php");
class connEmail{
    private $_mail = null;
    private $_header = false;
    private $_footer = false;
    public function __construct() {
        $this->_mail             = new PHPMailer();
        $this->_mail->IsSMTP(); // telling the class to use SMTP
        $this->_mail->Host       = "pro.turbo-smtp.com"; // SMTP server
        $this->_mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
        // 1 = errors and messages
        // 2 = messages only
        $this->_mail->SMTPAuth   = true;                  // enable SMTP authentication
        $this->_mail->SMTPSecure = 'ssl';
        $this->_mail->Port       = 465;                    // set the SMTP port for the GMAIL server
        $this->_mail->Username   = "inventarios@ffactoryeu.com"; // SMTP account username
        $this->_mail->Password   = "3tJNYFf1";        // SMTP account password
        $this->_mail->SetFrom('info@studiofcentroamerica.com');
        $this->_mail->Subject    = "Email comunicaciones";
        $this->_mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
        
    } 
    
    /**
     * set subject del email
     * 
     * @param type $subject
     * @throws Exception
     */
    public function setAsunto($subject){
        try{
            $this->_mail->Subject = $subject;
        } catch (Exception $ex) {
            throw $ex;
        }
    }
    
    /**
     * set archivo adjunto
     * 
     * @author john jairo cortes garcia <john.cortes@syslab.so>
     * @param type $rutaFile
     * @throws Exception
     */
    public function setAdjunto($rutaFile){
        try{
            $this->_mail->AddAttachment($rutaFile);      // attachment
        } catch (Exception $ex) {
            throw $ex;
        }
    }

    /**
     * set destinatario
     * 
     * @author john jairo cortes garcia <john.cortes@syslab.so>
     * @param type $address
     * @throws Exception
     */
    public function setDestinatario($address){
        try{
            $this->_mail->AddAddress($address);
        } catch (Exception $ex) {
            throw $ex;
        }
    }
    
    /**
     * set reply to
     * 
     * @author john jairo cortes garcia <john.cortes@syslab.so>
     * @param type $emailR
     * @throws Exception
     */
    public function setReplyTo($emailR){
        try{
            $this->_mail->AddReplyTo($emailR);
        } catch (Exception $ex) {
            throw $ex;
        }
    }
    
    /**
     * enviar email
     * 
     * @author john jairo cortes garcia <john.cortes@syslab.so>
     * @param type $body
     * @return boolean
     * @throws Exception
     */
    public function sendEmail($body){
        try{
            $body = eregi_replace("[\]",'',$body);
            $this->_mail->IsHTML(true); // if you are going to send HTML formatted emails
            // create header
            if($this->_header){
                $body = $this->_header.$body;
            }
            // create footer
            if($this->_footer){
                $body = $body.$this->_footer;
            }
            $this->_mail->MsgHTML($body);
            if(!$this->_mail->Send()) {
                echo "Mailer Error: " . $this->_mail->ErrorInfo;
                $this->resetDestinatarios();
                return false;
            }
            $this->resetDestinatarios();
            return true;
        } catch (Exception $ex) {
            throw $ex;
        }
    }
    
    /**
     * 
     * @param type $strhtml
     * @throws Exception
     */
    public function setHeader($strhtml){
        try{
            $this->_header = $strhtml;
        } catch (Exception $ex) {
            throw $ex;
        }
    }
    
    /**
     * 
     * @param type $strhtml
     * @throws Exception
     */
    public function setFooter($strhtml){
        try{
            $this->_footer = $strhtml;
        } catch (Exception $ex) {
            throw $ex;
        }
    }
    
    /**
     * reset destinatarios
     * 
     * @author john jairo cortes garcia <john.cortes@syslab.so>
     * @return boolean
     * @throws Exception
     */
    private function resetDestinatarios(){
        try{
            $this->_mail->ClearAddresses();  // each AddAddress add to list
            $this->_mail->ClearCCs();
            $this->_mail->ClearBCCs();
            
            return true;
        } catch (Exception $ex) {
            throw $ex;
        }
    }
    
}