<?php
require_once 'Zend/Mail.php';
/**
 * Goweb Sitemanager 5 Framework
 *
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
 */

/**
 *
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
 */
class Goweb_Mail extends Zend_Mail{
    
    public function __construct($charset = CHARSET){
        $this->_config = Goweb::registry('config');
        $this->setFrom($this->_config->mail->from_email, $this->_config->mail->from_name);
        if (!self::$_defaultTransport instanceof Zend_Mail_Transport_Abstract) {
            goweb::loadClass('Zend_Mail_Transport_Smtp');
            if ($this->_config->mail->smtp){
                $options = array(
                	'auth' => 'login',
                	'username' => $this->_config->mail->username,
                	'password' => $this->_config->mail->password);
            } else {
                $options = array();
            }
            
            $options['port'] = $this->_config->mail->port;
            if ($this->_config->mail->port == 465 || $this->_config->mail->port == 587) $options['ssl'] = 'ssl';
            
            $tr = new Zend_Mail_Transport_Smtp($this->_config->mail->host,$options);
            Zend_Mail::setDefaultTransport($tr);
        }
        parent::__construct($charset);
    }
    
    public function setSupportMail(){
        if($this->_config->mail->to_suporte){
            $this->addTo($this->_config->mail->to_suporte, html_entity_decode('Apoio T&eacute;cnico - Goweb', ENT_NOQUOTES, CHARSET));
            if(strpos($this->_config->mail->to_suporte, "suporte_sm5@goweb.pt") === false) $this->addBcc('suporte_sm5@goweb.pt', html_entity_decode('Apoio T&eacute;cnico - Goweb', ENT_NOQUOTES, CHARSET));
        }
        else{
            $this->addTo('suporte_sm5@goweb.pt', html_entity_decode('Apoio T&eacute;cnico - Goweb', ENT_NOQUOTES, CHARSET));
        }
    }
}