00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 class Mage_Core_Model_Email_Template extends Varien_Object
00046 {
00047
00048
00049
00050 const TYPE_TEXT = 1;
00051 const TYPE_HTML = 2;
00052
00053
00054
00055
00056
00057 const XML_PATH_TEMPLATE_EMAIL = 'global/template/email';
00058
00059 protected $_templateFilter;
00060 protected $_preprocessFlag = false;
00061 protected $_mail;
00062
00063 static protected $_defaultTemplates;
00064
00065
00066
00067
00068
00069
00070 protected $_designConfig;
00071
00072
00073
00074
00075
00076
00077 public function getResource()
00078 {
00079 return Mage::getResourceSingleton('core/email_template');
00080 }
00081
00082
00083
00084
00085
00086
00087 public function getMail()
00088 {
00089 if (is_null($this->_mail)) {
00090 $this->_mail = new Zend_Mail('utf-8');
00091 }
00092 return $this->_mail;
00093 }
00094
00095
00096
00097
00098
00099
00100
00101 public function setTemplateFilter(Varien_Filter_Template $filter)
00102 {
00103 $this->_templateFilter = $filter;
00104 return $this;
00105 }
00106
00107
00108
00109
00110
00111
00112 public function getTemplateFilter()
00113 {
00114 if (empty($this->_templateFilter)) {
00115 $this->_templateFilter = Mage::getModel('core/email_template_filter');
00116 $this->_templateFilter->setUseAbsoluteLinks($this->getUseAbsoluteLinks());
00117 }
00118 return $this->_templateFilter;
00119 }
00120
00121
00122
00123
00124
00125
00126
00127 public function load($templateId)
00128 {
00129 $this->addData($this->getResource()->load($templateId));
00130 return $this;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139 public function loadByCode($templateCode)
00140 {
00141 $this->addData($this->getResource()->loadByCode($templateCode));
00142 return $this;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 public function loadDefault($templateId, $locale=null)
00152 {
00153 $defaultTemplates = self::getDefaultTemplates();
00154 if (!isset($defaultTemplates[$templateId])) {
00155 return $this;
00156 }
00157
00158 $data = &$defaultTemplates[$templateId];
00159 $this->setTemplateType($data['type']=='html' ? self::TYPE_HTML : self::TYPE_TEXT);
00160
00161 $templateText = Mage::app()->getTranslator()->getTemplateFile(
00162 $data['file'], 'email', $locale
00163 );
00164
00165 if (preg_match('/<!--@subject\s*(.*?)\s*@-->/', $templateText, $matches)) {
00166 $this->setTemplateSubject($matches[1]);
00167 $templateText = str_replace($matches[0], '', $templateText);
00168 }
00169
00170
00171
00172
00173 $templateText = preg_replace('#\{\*.*\*\}#suU', '', $templateText);
00174
00175 $this->setTemplateText($templateText);
00176 $this->setId($templateId);
00177
00178 return $this;
00179 }
00180
00181
00182
00183
00184
00185
00186 static public function getDefaultTemplates()
00187 {
00188 if(is_null(self::$_defaultTemplates)) {
00189 self::$_defaultTemplates = Mage::getConfig()->getNode(self::XML_PATH_TEMPLATE_EMAIL)->asArray();
00190 }
00191
00192 return self::$_defaultTemplates;
00193 }
00194
00195
00196
00197
00198
00199
00200 static public function getDefaultTemplatesAsOptionsArray()
00201 {
00202 $options = array(
00203 array('value'=>'', 'label'=> '')
00204 );
00205
00206 foreach (self::getDefaultTemplates() as $templateId=>$value) {
00207 $options[] = array('value'=>$templateId, 'label'=>$value['label']);
00208 }
00209
00210 return $options;
00211 }
00212
00213
00214
00215
00216
00217 public function getId()
00218 {
00219 return $this->getTemplateId();
00220 }
00221
00222
00223
00224
00225
00226 public function setId($value)
00227 {
00228 return $this->setTemplateId($value);
00229 }
00230
00231
00232
00233
00234
00235
00236 public function isValidForSend()
00237 {
00238 return !Mage::getStoreConfigFlag('system/smtp/disable')
00239 && $this->getSenderName()
00240 && $this->getSenderEmail()
00241 && $this->getTemplateSubject();
00242 }
00243
00244
00245
00246
00247
00248
00249 public function isPlain()
00250 {
00251 return $this->getTemplateType() == self::TYPE_TEXT;
00252 }
00253
00254
00255
00256
00257 public function save()
00258 {
00259 $this->getResource()->save($this);
00260 return $this;
00261 }
00262
00263
00264
00265
00266
00267
00268
00269 public function getProcessedTemplate(array $variables = array())
00270 {
00271 $processor = $this->getTemplateFilter();
00272 $processor->setUseSessionInUrl(false);
00273
00274 if(!$this->_preprocessFlag) {
00275 $variables['this'] = $this;
00276 }
00277
00278 $processor->setIncludeProcessor(array($this, 'getInclude'))
00279 ->setVariables($variables);
00280
00281 $this->_applyDesignConfig();
00282 try{
00283 $processedResult = $processor->filter($this->getTemplateText());
00284 }
00285 catch ( Exception $e) {
00286 $this->_cancelDesignConfig();
00287 throw $e;
00288 }
00289 $this->_cancelDesignConfig();
00290 return $processedResult;
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300 public function getInclude($template, array $variables)
00301 {
00302 $thisClass = __CLASS__;
00303 $includeTemplate = new $thisClass();
00304
00305 $includeTemplate->loadByCode($template);
00306
00307 return $includeTemplate->getProcessedTemplate($variables);
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 public function send($email, $name=null, array $variables = array())
00319 {
00320 if(!$this->isValidForSend()) {
00321 return false;
00322 }
00323
00324 if (is_null($name)) {
00325 $name = substr($email, 0, strpos($email, '@'));
00326 }
00327
00328 $variables['email'] = $email;
00329 $variables['name'] = $name;
00330
00331 ini_set('SMTP', Mage::getStoreConfig('system/smtp/host'));
00332 ini_set('smtp_port', Mage::getStoreConfig('system/smtp/port'));
00333
00334 $mail = $this->getMail();
00335 if (is_array($email)) {
00336 foreach ($email as $emailOne) {
00337 $mail->addTo($emailOne, $name);
00338 }
00339 } else {
00340 $mail->addTo($email, '=?utf-8?B?'.base64_encode($name).'?=');
00341 }
00342
00343 $this->setUseAbsoluteLinks(true);
00344 $text = $this->getProcessedTemplate($variables, true);
00345
00346 if($this->isPlain()) {
00347 $mail->setBodyText($text);
00348 } else {
00349 $mail->setBodyHTML($text);
00350 }
00351
00352
00353
00354 $mail->setSubject('=?utf-8?B?'.base64_encode($this->getProcessedTemplateSubject($variables)).'?=');
00355 $mail->setFrom($this->getSenderEmail(), $this->getSenderName());
00356
00357 try {
00358 $mail->send();
00359 $this->_mail = null;
00360 }
00361 catch (Exception $e) {
00362 return false;
00363 }
00364
00365 return true;
00366 }
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 public function sendTransactional($templateId, $sender, $email, $name, $vars=array(), $storeId=null)
00380 {
00381 $this->setSentSuccess(false);
00382 if (($storeId === null) && $this->getDesignConfig()->getStore()) {
00383 $storeId = $this->getDesignConfig()->getStore();
00384 }
00385
00386 if (is_numeric($templateId)) {
00387 $this->load($templateId);
00388 } else {
00389 $localeCode = Mage::getStoreConfig('general/locale/code', $storeId);
00390 $this->loadDefault($templateId, $localeCode);
00391 }
00392
00393 if (!$this->getId()) {
00394 throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid transactional email code: '.$templateId));
00395 }
00396
00397 if (!is_array($sender)) {
00398 $this->setSenderName(Mage::getStoreConfig('trans_email/ident_'.$sender.'/name', $storeId));
00399 $this->setSenderEmail(Mage::getStoreConfig('trans_email/ident_'.$sender.'/email', $storeId));
00400 } else {
00401 $this->setSenderName($sender['name']);
00402 $this->setSenderEmail($sender['email']);
00403 }
00404
00405 $this->setSentSuccess($this->send($email, $name, $vars));
00406 return $this;
00407 }
00408
00409
00410
00411
00412 public function delete()
00413 {
00414 $this->getResource()->delete($this->getId());
00415 $this->setId(null);
00416 return $this;
00417 }
00418
00419
00420
00421
00422
00423
00424
00425 public function getProcessedTemplateSubject(array $variables)
00426 {
00427 $processor = $this->getTemplateFilter();
00428
00429 if(!$this->_preprocessFlag) {
00430 $variables['this'] = $this;
00431 }
00432
00433 $processor->setVariables($variables);
00434
00435 $this->_applyDesignConfig();
00436 try{
00437 $processedResult = $processor->filter($this->getTemplateSubject());
00438 }
00439 catch ( Exception $e) {
00440 $this->_cancelDesignConfig();
00441 throw $e;
00442 }
00443 $this->_cancelDesignConfig();
00444 return $processedResult;
00445 }
00446
00447
00448
00449
00450
00451
00452
00453 public function setDesignConfig(array $config)
00454 {
00455 $this->getDesignConfig()->setData($config);
00456 return $this;
00457 }
00458
00459
00460
00461
00462
00463
00464 public function getDesignConfig()
00465 {
00466 if(is_null($this->_designConfig)) {
00467 $this->_designConfig = new Varien_Object();
00468 }
00469 return $this->_designConfig;
00470 }
00471
00472
00473
00474
00475
00476
00477 protected function _applyDesignConfig()
00478 {
00479 if ($this->getDesignConfig()) {
00480 $design = Mage::getDesign();
00481 $this->getDesignConfig()
00482 ->setOldArea($design->getArea())
00483 ->setOldStore($design->getStore());
00484
00485 if ($this->getDesignConfig()->getArea()) {
00486 Mage::getDesign()->setArea($this->getDesignConfig()->getArea());
00487 }
00488
00489 if ($this->getDesignConfig()->getStore()) {
00490 Mage::app()->getLocale()->emulate($this->getDesignConfig()->getStore());
00491 $design->setStore($this->getDesignConfig()->getStore());
00492 $design->setTheme('');
00493 $design->setPackageName('');
00494 }
00495
00496 }
00497 return $this;
00498 }
00499
00500
00501
00502
00503
00504
00505 protected function _cancelDesignConfig()
00506 {
00507 if ($this->getDesignConfig()) {
00508 if ($this->getDesignConfig()->getOldArea()) {
00509 Mage::getDesign()->setArea($this->getDesignConfig()->getOldArea());
00510 }
00511
00512 if ($this->getDesignConfig()->getOldStore()) {
00513 Mage::getDesign()->setStore($this->getDesignConfig()->getOldStore());
00514 Mage::getDesign()->setTheme('');
00515 Mage::getDesign()->setPackageName('');
00516 }
00517 }
00518 Mage::app()->getLocale()->revert();
00519 return $this;
00520 }
00521
00522 public function addBcc($bcc)
00523 {
00524 if (is_array($bcc)) {
00525 foreach ($bcc as $email) {
00526 $this->getMail()->addBcc($email);
00527 }
00528 }
00529 elseif ($bcc) {
00530 $this->getMail()->addBcc($bcc);
00531 }
00532 return $this;
00533 }
00534
00535
00536
00537
00538
00539
00540
00541 public function setReturnPath($email)
00542 {
00543 $this->getMail()->setReturnPath($email);
00544 return $this;
00545 }
00546
00547
00548
00549
00550
00551
00552
00553 public function setReplyTo($email)
00554 {
00555 $this->getMail()->addHeader('Reply-To', $email);
00556 return $this;
00557 }
00558 }