From 95803eba882a4f72623390c4b084512975a9fcf2 Mon Sep 17 00:00:00 2001 From: Vinai Kopp Date: Wed, 11 Jul 2012 15:51:19 +0200 Subject: [PATCH] Add events to transactional email sending The purpose of the events is to enable modules to - add attachments - change email templates - modify template variables - add template variables - modify email content Also, the patch makes the mailer class factory method configurable. The purpose is to enable modules to use a diferent mailer without reverting to a rewrite of Mage_Core_Model_Email_Template. Additionaly the undeclared dependency on Mage_Newsletter was removed from Mage_Core_Model_Email_Template, and an event observer added to the newsletter module taking care the business logic is not changed. --- app/code/core/Mage/Core/Helper/Mail.php | 17 ++++++ .../core/Mage/Core/Model/Email/Template.php | 55 +++++++++++++------ app/code/core/Mage/Core/etc/config.xml | 8 ++- .../core/Mage/Newsletter/Model/Observer.php | 19 +++++++ app/code/core/Mage/Newsletter/etc/config.xml | 10 ++++ 5 files changed, 92 insertions(+), 17 deletions(-) create mode 100644 app/code/core/Mage/Core/Helper/Mail.php diff --git a/app/code/core/Mage/Core/Helper/Mail.php b/app/code/core/Mage/Core/Helper/Mail.php new file mode 100644 index 0000000000000..1597a4ae855f9 --- /dev/null +++ b/app/code/core/Mage/Core/Helper/Mail.php @@ -0,0 +1,17 @@ +getNode(self::XML_PATH_MAIL_FACTORY); + $parts = explode('::', (string) $factory); + if (count($parts) != 2) { + throw new Exception(sprintf('Failed to get mail factory class and method.')); + } + $mailer = call_user_func(array(Mage::helper($parts[0]), $parts[1])); + return $mailer; } /** @@ -349,25 +359,31 @@ public function getProcessedTemplate(array $variables = array()) $variables['this'] = $this; } - if (isset($variables['subscriber']) && ($variables['subscriber'] instanceof Mage_Newsletter_Model_Subscriber)) { - $processor->setStoreId($variables['subscriber']->getStoreId()); - } - - if (!isset($variables['logo_url'])) { - $variables['logo_url'] = $this->_getLogoUrl($processor->getStoreId()); - } - if (!isset($variables['logo_alt'])) { - $variables['logo_alt'] = $this->_getLogoAlt($processor->getStoreId()); - } - - $processor->setIncludeProcessor(array($this, 'getInclude')) - ->setVariables($variables); + $processor->setIncludeProcessor(array($this, 'getInclude')); $this->_applyDesignConfig(); $storeId = $this->getDesignConfig()->getStore(); try { - $processedResult = $processor->setStoreId($storeId) - ->filter($this->getPreparedTemplateText()); + $processor->setStoreId($storeId); + $transport = new Varien_Object(array( + 'variables' => new Varien_Object($variables), + 'template_text' => $this->getPreparedTemplateText(), + 'store_id' => $storeId + )); + // Use observer to modify variables and template processor settings + Mage::dispatchEvent('email_template_filter_before', array( + 'processor' => $processor, + 'transport' => $transport + )); + if (!isset($variables['logo_url'])) { + $variables['logo_url'] = $this->_getLogoUrl($transport->getStoreId()); + } + if (!isset($variables['logo_alt'])) { + $variables['logo_alt'] = $this->_getLogoAlt($transport->getStoreId()); + } + + $processedResult = $processor->setVariables($transport->getVariables()->getData()) + ->filter($transport->getTemplateText()); } catch (Exception $e) { $this->_cancelDesignConfig(); throw $e; @@ -486,6 +502,13 @@ public function send($email, $name = null, array $variables = array()) $result = false; $this->_sendingException = null; try { + // Note: the email body already has been processed, changes to variables will have no effect + Mage::dispatchEvent('email_template_send_before', array( + 'mailer' => $mail, + 'variables' => new Varien_Object($variables), + 'template' => $this, + 'store_id' => $this->getDesignConfig()->getStore() + )); $mail->send(); $result = true; } catch (Exception $e) { diff --git a/app/code/core/Mage/Core/etc/config.xml b/app/code/core/Mage/Core/etc/config.xml index 2cd9c2937e39a..09b4127c40a4f 100644 --- a/app/code/core/Mage/Core/etc/config.xml +++ b/app/code/core/Mage/Core/etc/config.xml @@ -89,7 +89,13 @@ 0 + + 1 + + + Mage_Core_Helper_Mail::getMailer + @@ -291,7 +297,7 @@ - dashboard + dashboard 0 diff --git a/app/code/core/Mage/Newsletter/Model/Observer.php b/app/code/core/Mage/Newsletter/Model/Observer.php index 2ec75112e2d6c..06133eef95505 100644 --- a/app/code/core/Mage/Newsletter/Model/Observer.php +++ b/app/code/core/Mage/Newsletter/Model/Observer.php @@ -69,4 +69,23 @@ public function scheduledSend($schedule) $collection->walk('sendPerSubscriber', array($countOfSubscritions)); } + + /** + * Set the subscriber store id on the transport object + * + * Set the subscriber store id on the transport object so the logo configuration for the + * subscriber store is read (which is not necessarily the current one). + * + * @param Varien_Object $observer + * @return Mage_Newsletter_Model_Observer + */ + public function emailTemplateFilterBefore($observer) + { + // Only match newsletter subscriber events + $subscriber = $observer->getTransport()->getVariables()->getSubscriber(); + if ($subscriber && $subscriber instanceof Mage_Newsletter_Model_Subscriber) { + $observer->getTransport()->setStoreId($subscriber->getStoreId()); + } + return $this; + } } diff --git a/app/code/core/Mage/Newsletter/etc/config.xml b/app/code/core/Mage/Newsletter/etc/config.xml index 7dd987c964799..98107000b0a7a 100644 --- a/app/code/core/Mage/Newsletter/etc/config.xml +++ b/app/code/core/Mage/Newsletter/etc/config.xml @@ -61,6 +61,16 @@ Mage_Newsletter_Model_Template_Filter + + + + + Mage_Newsletter_Model_Observer + emailTemplateFilterBefore + + + +