Skip to content

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 i18next with i18next-fs-backend and loads from locales/<lng>/<ns>.json.
  • Provide your own initialized instance via the i18next option to reuse an existing app i18n instance.
  • Use {{t "namespace:key"}} or {{__ "namespace:key"}} inside templates. The locale comes from mail.data.locale.

Subject translation:

  • Provide mail.data.subjectKey to use a specific key, e.g. emails:welcome.subject.
  • Or enable autoSubject (default true) to use <subjectNamespace>:<template>.subject (default namespace emails).

Mail Data

When calling transporter.sendMail(...), set these fields:

  • template: base name of the template file without extension.
  • locale: optional; defaults to defaultLocale.
  • context (or locals): data for Handlebars and i18next interpolation.
  • subjectKey: optional i18next key for the subject.

Helpers

  • Built-in helpers: t and __ for translations.
  • You can register additional helpers via helpers option:
transporter.use('compile', mjmlI18nextCompiler({
  /* ... */
  helpers: {
    upper: (s) => String(s).toUpperCase(),
  },
}));

Then use in templates: {{upper name}}.