<?php
class MailManComponent extends Object {     
    
	/**
	 * PHPMailer object.
	 * 
	 * @access private
	 * @var object
	 */
	private $m; 
	
	private $controller;
	
	private $defaultReplyToAddress = 'help@stoned.com';
	 
		 
	public function startup(&$controller) {
    
		$this->controller = $controller;
     	$this->init();
    }
    
    /**
     * Creates the PHPMailer object and sets default values.
     * Must be called before working with the component!
     *
     * @access public
     * @return void
     */
    private function init() {
        
		vendor('phpmailer');
        $this->m = new PHPMailer();
        
        // Set default PHPMailer variables (see PHPMailer API for more info)
        $this->AddReplyTo( $this->defaultReplyToAddress );
		$this->SetFrom($this->defaultReplyToAddress, 'stoned.com');
    }
	
	/**
     * Set the body of the email
     */
    public function setBody($template, $layout = null) {
      
	  	$this->Body = $this->_render( 'app/views/email_templates/'.$template, $layout);
		$this->isHtml();
    }

    /**
     * Set the AltBody, like for the text in HTML+TXT emails
     */
    public function setAltBody($template, $layout = null) {
      
	  	$this->AltBody = $this->_render( 'app/views/email_templates/'.$template, $layout);
    }

    private function _render($template, $layout) {
		 
		ob_start();
		$this->controller->autoRender = false;
		$this->controller->render(null, $layout, $template);
		return ob_get_clean();
    } 
	 
	public function __set($name, $value) {
		
        $this->m->{$name} = $value;
    }
    
    public function __get($name) {
		
        if(isset($this->m->{$name})) {
            return $this->m->{$name};
        }
    }
             
    public function __call($method, $args) {
		
        if(method_exists($this->m, $method)) {
            return call_user_func_array(array($this->m, $method), $args);
        }
    }
}
?>