6

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());
        $mailer->addEmailInfo($emailInfo);

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


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

Comments in code.

It doesn't work. Any ideas will be appreciated

1
  • app/code/core/Mage/Core/Model/Email/Template.php Check send function there you can echo html of email and you can save it Commented Jan 13, 2016 at 10:02

2 Answers 2

1

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")));
4
  • I want to save content of all HTML code in file on my harddisk for example :)
    – corey
    Commented Jan 13, 2016 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. Commented Jan 13, 2016 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
    Commented Jan 13, 2016 at 10:18
  • Ok, I got it. It was simple: $mailer->getProcessedTemplate($templateVariables) instead of create new variable $emailTemplate
    – corey
    Commented Jan 13, 2016 at 10:26
1

Have you considered installing an extension such as this

https://www.yireo.com/software/magento-extensions/emailtester

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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