Usage¶
Templates¶
Place your email templates under templatesDir. Supported extensions (resolved in order):
<name>.mjml.hbs<name>.hbs.mjml<name>.mjml<name>.hbs
Example welcome.mjml.hbs:
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-text font-size="22px" font-weight="bold">{{t "emails:welcome.title" name=name}}</mj-text>
<mj-text>{{t "emails:welcome.intro" name=name}}</mj-text>
{{> footer }}
</mj-column>
</mj-section>
</mj-body>
</mjml>
Partials can be stored in partialsDir and referenced with {{> partialName }}.
Translations (i18next)¶
- By default, the plugin initializes
i18nextwithi18next-fs-backendand loads fromlocales/<lng>/<ns>.json. - Provide your own initialized instance via the
i18nextoption to reuse an existing app i18n instance. - Use
{{t "namespace:key"}}or{{__ "namespace:key"}}inside templates. The locale comes frommail.data.locale.
Subject translation:
- Provide
mail.data.subjectKeyto use a specific key, e.g.emails:welcome.subject. - Or enable
autoSubject(default true) to use<subjectNamespace>:<template>.subject(default namespaceemails).
Mail Data¶
When calling transporter.sendMail(...), set these fields:
template: base name of the template file without extension.locale: optional; defaults todefaultLocale.context(orlocals): data for Handlebars and i18next interpolation.subjectKey: optional i18next key for the subject.
Helpers¶
- Built-in helpers:
tand__for translations. - You can register additional helpers via
helpersoption:
transporter.use('compile', mjmlI18nextCompiler({
/* ... */
helpers: {
upper: (s) => String(s).toUpperCase(),
},
}));
Then use in templates: {{upper name}}.