<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Core
 * @copyright   Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Possible data fields:
 *
 * - subject
 * - to
 * - from
 * - body
 * - template (file name)
 * - module (for template)
 *
 */
class Mage_Core_Model_Email extends Varien_Object {
	protected $_tplVars = array ();
	protected $_block;
	
	public function __construct() {
		// TODO: move to config
		$this->setFromName ( 'Magento' );
		$this->setFromEmail ( 'magento@varien.com' );
		$this->setType ( 'text' );
	}
	
	public function setTemplateVar($var, $value = null) {
		if (is_array ( $var )) {
			foreach ( $var as $index => $value ) {
				$this->_tplVars [$index] = $value;
			}
		} else {
			$this->_tplVars [$var] = $value;
		}
		return $this;
	}
	
	public function getTemplateVars() {
		return $this->_tplVars;
	}
	
	public function getBody() {
		$body = $this->getData ( 'body' );
		if (empty ( $body ) && $this->getTemplate ()) {
			$this->_block = Mage::getModel ( 'core/layout' )->createBlock ( 'core/template', 'email' )->setArea ( 'frontend' )->setTemplate ( $this->getTemplate () );
			foreach ( $this->getTemplateVars () as $var => $value ) {
				$this->_block->assign ( $var, $value );
			}
			$this->_block->assign ( '_type', strtolower ( $this->getType () ) )->assign ( '_section', 'body' );
			$body = $this->_block->toHtml ();
		}
		return $body;
	}
	
	public function getSubject() {
		$subject = $this->getData ( 'subject' );
		if (empty ( $subject ) && $this->_block) {
			$this->_block->assign ( '_section', 'subject' );
			$subject = $this->_block->toHtml ();
		}
		return $subject;
	}
	
	public function send() {
		if (! Mage::getStoreConfigFlag ( 'system/smtp/email_agent' )) {
			return $this;
		}
		
		$mail = new Zend_Mail ( 'utf-8' );
		
		if (strtolower ( $this->getType () ) == 'html') {
			$mail->setBodyHtml ( $this->getBody () );
		} else {
			$mail->setBodyText ( $this->getBody () );
		}
		
		$mail->setFrom ( $this->getFromEmail (), $this->getFromName () )->addTo ( $this->getToEmail (), $this->getToName () )->setSubject ( $this->getSubject () );
		
		if (Mage::getStoreConfig ( 'system/smtp/email_agent' ) == 2) {
			//2为使用smtp service发送邮件
			$transport = Mage::helper ( 'adminhtml' )->getTransport ();
			$mail->send ( $transport );
			return $this;
		}
		
		$mail->send ();
		
		return $this;
	}
}
