Notice: You are browsing the documentation for PrestaShop 9, which is currently in development.
You might want to read the documentation for the current version, PrestaShop 8. Read the current version of this page
PrestaShop’s email notification system is based on static template files, one for each kind of message (like account creation, order confirmation, etc), which are stored in the mails
folder. In order to localize these messages, PrestaShop needs a copy of each template, translated to every supported language. Shops download these translated versions when installing a new language pack.
This system has a few drawbacks. Shops need to download email packs every time a new language is installed, and if a template had not been translated to that language, you ended up with emails in English. This gets even more complicated when you use an email theme (from Addons or a freelance designer) which is therefore limited to the languages the authors translated by themselves. Also, any customization performed in a template has to be replicated for every installed language.
This feature was significantly revamped in PrestaShop 1.7. To avoid introducing breaking changes, the system still relies on static email templates, which are used by the Mail::send
method. However, instead of downloading translated copies of each template, these files are now dynamically generated whenever you install a new language, using base templates, or layouts.
This is the first step to improving the email system in PrestaShop. As the feature gets further improved we will add more advanced customization for email templates.
Terms like “layouts”, “templates” and “themes” can be confusing, so let’s clarify them:
Mail themes are groups of Layouts with a given style, stored in the mails/themes/
directory.
You can have many mail themes installed, but only one active at a time.
Layouts are files that will be rendered using Twig to generate ready-to-use mail Templates.
They can use logic statements, translate wordings, extend other base layouts or include components. They are the basic files that make up your Mail theme.
Templates are static, translated files (html or txt) that will be generated by MailThemeGenerator
from Layouts of a given Mail theme in a given language.
They contain no logic and are translated for ONE language. These are the files used by the Mail
class when you send a mail and are located in the mails/
directory, grouped by language (mails/en/
, mails/fr/
, …).
The new email themes layout files are stored in the mails/themes/
folder. PrestaShop is bundled with two email themes:
Each of these folder contains twig layouts which are organized in a conventional way:
...
├── mails
| ├── themes
| | ├── modern
| | | ├── assets # Contains the assets used in your layouts (optional)
| | | ├── components # Contains block parts or base layouts for your email theme (optional)
| | | | ├── footer.html.twig
| | | | ├── layout.html.twig
| | | ├── core # Contains layouts for Core transactional mails
| | | | ├── account.html.twig # HTML layout for "account" transactional mail
| | | | ├── account.txt.twig # TXT layout for "account" transactional mail
| | | | ├── bankwire.html.twig # HTML layout for "bankwire" transactional mail
| | | | ├── cheque.txt.twig # TXT layout for "cheque" transactional mail
| | | | ├── contact.html.twig # HTML layout for "contact" transactional mail
| | | ├── modules # Contains layouts specific to a given module
| | | | ├── followup # Module name
| | | | | ├── followup_1.html.twig
| | | | | ├── followup_2.html.twig
| | | | ├── ps_emailalerts # Module name
| | | | | ├── new_order.html.twig
| | | | | ├── followup_2.html.twig
...
As you can see there are two types of layouts, one for each type of template:
The layout name should respect the following convention: {layout_name}.{layout_type}.twig
For example:
account.html.twig
: layout for the account
mail template in its html
versioncheque.txt.twig
: layout for the cheque
mail template in its txt
versionAs you may have noticed, some of our layouts have both types (e.g.: account
) whereas others only have html or txt type (bankwire
, cheque
, …).
This is because you are not forced to define both types as they will be used as a fallback for each other:
PrestaShop uses objects to manipulate email themes and layouts, they implement the following interfaces:
PrestaShop\PrestaShop\Core\MailTemplate\ThemeInterface
– Describes a theme and provides a list of its layoutsPrestaShop\PrestaShop\Core\MailTemplate\Layout\LayoutInterface
– Describes a theme layout (name, file paths, related module…)These interfaces have a corresponding collection that is used in the core services and provided via hooks:
PrestaShop\PrestaShop\Core\MailTemplate\ThemeCollectionInterface
PrestaShop\PrestaShop\Core\MailTemplate\Layout\LayoutCollectionInterface
Of course, PrestaShop provides concrete implementations which you are encouraged to reuse:
PrestaShop\PrestaShop\Core\MailTemplate\Theme
PrestaShop\PrestaShop\Core\MailTemplate\ThemeCollection
PrestaShop\PrestaShop\Core\MailTemplate\Layout\Layout
PrestaShop\PrestaShop\Core\MailTemplate\Layout\LayoutCollection
The templates from layouts generation workflow is a bit complex, here are the main components:
MailTemplateGenerator
.ThemeCollection
, which is built by scanning mail themes in folders.ThemeInterface
for the requested LanguageInterface
.TransformationInterface
.As you can see in the workflow, the email generation process includes a few hooks that allow you to include your own themes, layouts, variables and transformations:
ThemeCollection
(add, remove, modify a theme or/and its layouts)Layout variables are NOT template variables
Always keep in mind that the variables provided by the LayoutVariablesBuilder
will only be available during template generation, meaning that their value will be fixed into the exported static templates, and won’t change dynamically when your emails are being sent. They should not be confused with template variables (like firstname,
lastname, …) which are replaced at the last moment when the email is sent by the Mail::send
function.
Here is a quick resume of the differences:
Layout | Template | |
---|---|---|
Renderer | Twig | Swift_Plugins_DecoratorPlugin |
Syntax | {{ variable }} |
{ variable } |
Interpreted | On generation | When the email is being sent |
Target | All users/customers | Specific user/action |
Use cases | Design, translations, … | Customization (user, order, …) |
One of the main advantages of email generation is the possibility to use translations in the layouts, here is an example:
<table width="100%">
<tr>
<td align="center" class="titleblock">
<font size="2" face="Open-sans, sans-serif" color="#555454">
<span class="title">{{ 'This is a translated string'|trans({}, 'EmailsBody', locale)|raw }}</span>
</font>
</td>
</tr>
</table>
So now that you know how the generation process works, you might wonder when exactly does it happen? There are a few cases when generation is automatic:
Language
class and more particularly during the calls to Language::downloadAndInstallLanguagePack
, Language::installLanguagePack
and of course Language::installEmailsLanguagePack
which is now deprecatedPrestaShopBundle\Install\Upgrade::run
when languages are updatedOf course sometimes you still want to manually generate your emails (new theme installed, changes in some layouts, …), then you can use:
prestashop:mail:generate
if you want to launch a CLI generationYour shop can only use one theme at a time, so if you go to the “Design > Email Theme” page you will be able to choose your default email theme. This default theme will be used each time the generation process is launched automatically (language installation, upgrade, …).
The modern theme is active by default. The classic theme was provided for backward compatibility and as an example.
Indeed when you select your default theme you simply update your configuration, so any future generation will use the theme you selected.
However no generation process is launched when you select a theme, so if you want to generate your emails with your newly selected theme you need to do it manually in the “Generate emails” form.
There are two possibilities for this issue:
1. Overwrite already-generated templates
As you may have noticed, the GenerateThemeMailTemplatesCommand
and the “Generate emails” form have an overwrite option. We need this option because some shops may have installed email themes, or customized their templates manually. For that reason by default the generation process does not export a template if it already exists.
If you want to replace the former templates you need to enable the overwrite
option.
2. Overwrite the shop theme’s mail templates
As you may know, your PrestaShop theme (the shop’s theme, not the email theme) can include mail templates which override the shop’s default ones. Those templates are not contained in the default mails
folder, but the in themes/{my_theme}/mails/
folder. Even if you generate your new mail theme (be it automatically or manually), the shop theme’s templates will have higher priority and will be used instead of your mail theme’s.
In that case, generate your mail theme manually and to select the shop theme you want to overwrite. Templates will be generated in its folder and will be used from now (don’t forget to enable the overwrite
option if you want to replace them).