1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Core
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Possible data fields:
30: *
31: * - subject
32: * - to
33: * - from
34: * - body
35: * - template (file name)
36: * - module (for template)
37: *
38: */
39: class Mage_Core_Model_Email extends Varien_Object
40: {
41: protected $_tplVars = array();
42: protected $_block;
43:
44: public function __construct()
45: {
46: // TODO: move to config
47: $this->setFromName('Magento');
48: $this->setFromEmail('magento@varien.com');
49: $this->setType('text');
50: }
51:
52: public function setTemplateVar($var, $value = null)
53: {
54: if (is_array($var)) {
55: foreach ($var as $index=>$value) {
56: $this->_tplVars[$index] = $value;
57: }
58: }
59: else {
60: $this->_tplVars[$var] = $value;
61: }
62: return $this;
63: }
64:
65: public function getTemplateVars()
66: {
67: return $this->_tplVars;
68: }
69:
70: public function getBody()
71: {
72: $body = $this->getData('body');
73: if (empty($body) && $this->getTemplate()) {
74: $this->_block = Mage::getModel('core/layout')->createBlock('core/template', 'email')
75: ->setArea('frontend')
76: ->setTemplate($this->getTemplate());
77: foreach ($this->getTemplateVars() as $var=>$value) {
78: $this->_block->assign($var, $value);
79: }
80: $this->_block->assign('_type', strtolower($this->getType()))
81: ->assign('_section', 'body');
82: $body = $this->_block->toHtml();
83: }
84: return $body;
85: }
86:
87: public function getSubject()
88: {
89: $subject = $this->getData('subject');
90: if (empty($subject) && $this->_block) {
91: $this->_block->assign('_section', 'subject');
92: $subject = $this->_block->toHtml();
93: }
94: return $subject;
95: }
96:
97: public function send()
98: {
99: if (Mage::getStoreConfigFlag('system/smtp/disable')) {
100: return $this;
101: }
102:
103: $mail = new Zend_Mail();
104:
105: if (strtolower($this->getType()) == 'html') {
106: $mail->setBodyHtml($this->getBody());
107: }
108: else {
109: $mail->setBodyText($this->getBody());
110: }
111:
112: $mail->setFrom($this->getFromEmail(), $this->getFromName())
113: ->addTo($this->getToEmail(), $this->getToName())
114: ->setSubject($this->getSubject());
115: $mail->send();
116:
117: return $this;
118: }
119: }
120: