We have a store for different countries that should use very similar mail templates. The logo - and of course - the texts have to be changed.

Is there a way around copying all templates and assigning them in the backend for each mail type that is sent?


I've done something like this a long long time ago (in a galaxy far far away).
The main idea is to replace the content of the e-mails with layout handles.
Magento already supports this. For example in the new order template there is this:

{{layout handle="sales_email_order_items" order=$order}} 

This piece of code looks for the layout handle sales_email_order_items and renders it in the e-mail as any frontend block(s). In the template used for rendering this block you have access to the translate function ($this->__()) so, for adding a new language you just have to translate the texts in that language.
You can even do something better. Usually all email templates have the same header and footer, so basically an e-mail template could look like this:

{{layout handle="custom_email_header"}}
{{layout handle="custom_email_content"}}<!-- this should be different for every e-mail -->
{{layout handle="custom_email_footer"}}

Here is an example. I chose the new account template because it's easier.
When I did this I've overwritten the Mage_Core_Model_Email_Template::loadDefault method to load the e-mail templates from a custom folder independent from the language so I won't have to add all the e-mails in my database (for portability) but it works if you add the e-mails in the DB.
So my new account template would look like this:

{{layout handle="custom_email_header" logo_url=$logo_url logo_alt=$logo_alt}}
{{layout handle="custom_email_account_new" customer=$customer store=$store}}
{{layout handle="cusom_email_footer" store=$store}}

The variable $customer=$customer (like others) is needed to pass the customer data that is passed to the e-mail, to my custom layout handle. In a layout file (create a new module for this) I had these handles:

    <block type="core/template" template="custom/email/header.phtml" />
    <block type="core/template" template="custom/email/footer.phtml" />
    <block type="core/template" template="custom/email/account/new.phtml" />

The file custom/email/header.phtml contains the header of the e-mail (copied and adapted from the default e-mail template):

<body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
<div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
<table cellspacing="0" cellpadding="0" border="0" height="100%" width="100%">
            <td align="center" valign="top" style="padding:20px 0 20px 0">
                <!-- [ header starts here] -->
                <table bgcolor="FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
                        <td valign="top">
                            <a href="<?php echo $this->getUrl('')?>"><img src="<?php echo $this->getLogoUrl())?>" alt="<?php echo $this->getLogoAlt()?>" style="margin-bottom:10px;" border="0"/></a></td>

The custom/email/footer.phtml contains the footer of the template:

                        <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA; text-align:center;"><center><p style="font-size:12px; margin:0;"><?php echo $this->__('Thank you again,');?> <strong><?php echo $this->getStore()->getFrontendName();?></strong></p></center></td>

custom/email/account/new.phtml looks like this:

                        <td valign="top">
                            <h1 style="font-size:22px; font-weight:normal; line-height:22px; margin:0 0 11px 0;""><?php echo $this->__('Dear %s', $customer->getName())?>,</h1>
                            <p style="font-size:12px; line-height:16px; margin:0 0 16px 0;"><?php echo $this->__('Welcome to %s', $this->getStore()->getFrontendName())?>. <?php echo $this->__('To log in when visiting our site just click <a href="%s" style="color:#1E7EC8;">Login</a> or <a href="%s" style="color:#1E7EC8;">My Account</a> at the top of every page, and then enter your e-mail address and password.', $this->getUrl('customer/account'), $this->getUrl('customer/account'))?></p>
                            <p style="border:1px solid #E0E0E0; font-size:12px; line-height:16px; margin:0; padding:13px 18px; background:#f9f9f9;">
                                <?php echo $this->__('Use the following values when prompted to log in:')?><br/>
                                <strong><?php echo $this->__('E-mail');?></strong>: <?php echo $customer->getEmail()?><br/>
                                <strong><?php echo $this->__('Password');?></strong>: <?php echo $customer->getPassword()?><p>
                            <p style="font-size:12px; line-height:16px; margin:0 0 8px 0;"><?php echo $this->__('When you log in to your account, you will be able to do the following:');?></p>
 <!-- Content removed here because I'm to lazy to add a translation function around every text --->
                            <p style="font-size:12px; line-height:16px; margin:0;"><?php echo $this->__('If you have any questions about your account or any other matter, please feel free to contact us at <a href="mailto:%s" style="color:#1E7EC8;">%s</a> or by phone at %s.', Mage::getStoreConfig('trans_email/ident_support/email', $this->getStore()->getId()), Mage::getStoreConfig('trans_email/ident_support/email', $this->getStore()->getId()), 
Mage::getStoreConfig('general/store_information/phone', $this->getStore()->getId()))?></p>

It's not necessarily a pretty solution, but it worked for me on a website with 6 languages, and when the customer requested changes on the e-mail templates (and he did...a lot of them) They were done in a few minutes.

The code above is written from memory, so it might have some errors, but you get the idea.

  • Awesome. Only that we have to replace the standard templates which use the mail-filter syntax by PHP code. But I like PHP code more.
    – Alex
    Jul 16 '13 at 14:36
  • 1
    I've used this on a few shops as well, it's a much better way of handling your mail templates. Small tip, you can also use static blocks this way! Perhaps that's easier for your client. Jul 16 '13 at 14:38
  • I use this method, but with static blocks. Highly approve, +1.
    – philwinkle
    Jul 16 '13 at 14:41
  • 1
    I guess it could work with static blocks, but I have a personal problem with static blocks. If you end up having to many of them managing them tends to become chaos. But I guess this is just my issue. Whatever floats your boat. I also don't like to give customers much control on the e-mail templates because that way I spend more time fixing what they break than I would do implementing their changes.
    – Marius
    Jul 16 '13 at 15:10
  • 1
    There is no hate. Just different opinions. And this is good. There are advantages and disadvantages for every method, so everybody is free to weigh them and pick the best for their needs.
    – Marius
    Jul 16 '13 at 20:48

You can create a new layout directive for example {{t text="Foobar"}} to translate texts.

  • 1
    You can use this extension. It does exactly that: github.com/miguelbalparda/MB_Translate. I don't know if it works for e-mail templates but it works nicely for cms blocks and pages. In case it doesn't you can make it work easily.
    – Marius
    Apr 16 '14 at 9:11

All this can be managed from the Admin Panel :)

  1. To make different templates for every language:
    System-> Transactional Emails->Add New Template. From the Template dropdown specify which email you need and from Locale specify the language you're referring to. -> Load Template. From the section below you can edit the template for the particular language. -> Save Template.
  2. To apply the changes:

    2.1. For custom header and footer go to: System -> Configuration -> Design -> Transactional Emails Change the store view to the one you want to apply the changes on. From here you can change the logo, header and footer.

    2.2 For content go to System -> Configuration -> Based on the template either

    • Customers -> Customer Configuration
    • Sales -> Sales Emails
    • Sales -> Shipment settings

Here is an article that covers the first part: http://www.templatemonster.com/help/magento-how-to-edit-store-emails.html

  • Does not answer the question. The question is how to avoid that copying and assigning.
    – Alex
    Jun 3 '15 at 12:19

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.