Downloading a Magento mail template and filling out its vars from code?

I upload my mail template as follows:

$mailTemplate = Mage::getModel('core/email_template'); $myTemplate = $mailTemplate->load($templateId); 

Now I can get the contents of the template using:

 $text = $myTemplate ->getData('template_text'); 

This works, but $ text still contains placeholders for variables, such as {{var myvar}} or {{store url = ""}}. Is there any way to fill in these placeholders when loading a template without sending mail? I want to show the text to the user, but with filled placeholders.

Possible?

Thanks:)

+4
source share
2 answers

Yes it is possible.

The Mage_Core_Model_Email_Template class has a getProcessedTemplate() method. You only need to pass the correct variables for your placeholders.

For example, if your template contains placeholders like this:

 {{var firstname}} {{var lastname}} 

you can use:

 $sTemplate = Mage::getModel('core/email_template') ->load($templateId) ->getProcessedTemplate(array( 'firstname' => 'John', 'lastname' => 'Doe' )); 

to allow your placeholders.

+7
source

To download an email template and populate it with variables, you can do the following:

  $emailTemplate = Mage::getModel('core/email_template') ->loadDefault('<your_email_template>'); //create template variables $emailTemplateVariables = array(); $emailTemplateVariables['firstname'] = <firstname_var>; $emailTemplateVariables['lastname'] = <lastname_var>; // in your tample tou can use {var firstname} and {var lastname} //fill template variables in email template $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables); $emailTemplate->setSenderName('<name>'); $emailTemplate->setSenderEmail('<emailaddress>'); $emailTemplate->setTemplateSubject($this->__('<your subject>')); //send mail $emailTemplate->send(<receiver_emailaddress>, <receiver_name>, $emailTemplateVariables); 

Hope someone can use it ,-)

Yours faithfully,

Martine

+2
source

Source: https://habr.com/ru/post/1393157/


All Articles