I would like to know if it's possible to get code html of transactional emails before send email to user with template variables etc...

I override _sendEmailTemplate and I try to get HTML code in this way:

protected function _sendEmailTemplate($template, $sender, $templateParams = array(), $storeId = null) {
        /** @var $mailer Mage_Core_Model_Email_Template_Mailer */
        $mailer = Mage::getModel('core/email_template_mailer');
        $emailInfo = Mage::getModel('core/email_info');
        $emailInfo->addTo($this->getEmail(), $this->getName());

        // Set all required params and send emails
        $mailer->setSender(Mage::getStoreConfig($sender, $storeId));
        $mailer->setTemplateId(Mage::getStoreConfig($template, $storeId));
        $emailTemplate = Mage::getModel('core/email_template')->loadByCode($template); // I try to get template

        $processedTemplate = $emailTemplate->getProcessedTemplate($templateParams); //set template variables and get html code
        return $this;

Comments in code.

It doesn't work. Any ideas will be appreciated

| improve this question | | | | |
  • app/code/core/Mage/Core/Model/Email/Template.php Check send function there you can echo html of email and you can save it – Rohit Kundale Jan 13 '16 at 10:02

It depends on what you want to do with the HTML code, but what I used to do to debug emails is log the email HTML code into a file.

Instead of using print_r, you can do:

Mage::log($processedTemplate, null, sprintf("email_%s.html", date("Ymd-his")));
| improve this answer | | | | |
  • I want to save content of all HTML code in file on my harddisk for example :) – corey Jan 13 '16 at 10:02
  • The $processedTemplate variable contains the HTML of the email, from that point you can do whatever you want with it, just ensure your email is still being sent after you wrote your code. – Raphael at Digital Pianism Jan 13 '16 at 10:12
  • However I execute your Mage::log after send email, I can't get properly my HTML code, I don't know why because I check new email_2016-0113-101610.html and it's doesn't contain nothing instead of DEBUG and it's all. I don't get HTML code. – corey Jan 13 '16 at 10:18
  • Ok, I got it. It was simple: $mailer->getProcessedTemplate($templateVariables) instead of create new variable $emailTemplate – corey Jan 13 '16 at 10:26

Have you considered installing an extension such as this


| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.