Skip to content

Editing email templates: subject, text, Markdown and HTML per language

Describes Templates Editor 0.2.0.

The Templates editor lets you change the email templates of your app from the admin: the subject, the plain text, the Markdown and the HTML of each email, one language at a time. The templates are files in your app’s code. The editor reads and writes those files.

Where to find it

Open Settings › Emails Transactional in the admin (/admin/settings/email-sending) and choose the Email templates tab.

The tab appears only when:

  • the module is switched on under Admin › Modules (Templates editor; it is off by default), and
  • your role has the Settings permission and the Templates editor permission. Owner has both. Admin gets the module’s permissions when PhoenixKit first finds the module, and you can take them away in the roles matrix.

Changing templates needs more: the Edit email templates permission, and a server where the host developer allowed editing. On other servers the tab shows everything read only and says why at the top:

  • Read only on this server: the templates are changed where editing is allowed and come here with the program. Editing is off on this server. This is the normal state in production: templates are changed on a development server, committed to git and deployed.
  • The templates are editable on this server, but not by you: changing them needs the “Edit email templates” permission.
  • The templates editor is switched off: the templates are shown read only.
  • A message that the page does not tell the editor who you are: a setup step is missing (see “For developers”).

If the host’s configuration has a problem, the tab shows The templates editor is not configured correctly. with a list of what is wrong, and no editor.

The top of the tab

  • A short introduction. On an editable server it reminds you that a saved change changes the file, and reaches other servers once it is committed to git.
  • Hints about the Fill text from content and Markdown → HTML buttons and the Fallback tab.
  • Your project’s own notes, if the developers put a README file in the templates folder. Read them: they say what only your project knows, such as which variables an email may use.
  • Show what is not committed to git (editable servers only): lists the template files changed on this server since the last commit, under Changed on this server and not yet committed to git:, or says Everything here is committed to git. The list refreshes after every change you make in the editor.

The template list

The left column lists the templates you may see, under Messages (your emails), Layouts, Module emails and Shared parts. Each entry shows its caption and its name. Click one to open it. With nothing to show, the column says No templates yet.

An email marked Not offered for sending: no subject or body in … lacks a subject or body in the main language, and won’t be offered for sending.

Reload reads the templates from disk again: use it after someone changed the files by hand or by a git pull.

Editing a template

An open template has a row of language tabs, one per language the site is set up for, plus Fallback. Fallback holds the files without a language: a language with no file of its own gets them. When an email’s fallback files would never be used, the tab is marked not used, a box says so, and Delete removes them.

Each language tab has the same fields:

  • Label: the caption in lists. Never sent.
  • Subject: one line.
  • Text: the plain-text body.
  • Markdown: optional, an alternative to HTML.
  • HTML: optional.

A field that has a file shows when it was last changed.

The buttons under the fields:

  • Save saves the fields you changed in this language. A field saved empty has its file deleted, so the email falls back to the next file in line (for example the Fallback one).
  • Fill text from content writes the Markdown, or the HTML when there is no Markdown, into Text as plain text.
  • Markdown → HTML writes the Markdown, rendered, into HTML, with the site’s current accent color written into it.

The two converter buttons don’t save. Check the result, then Save.

Each language is saved on its own. Switching to another tab or template drops changes you haven’t saved, without asking.

Variables

The Variables panel lists the placeholders this kind of template can use, such as {{user_email}}, with a sample value and, if the developers added one, a description. Click a variable to put it where the cursor is in the field you were typing in. Some are marked Markdown only or rendered HTML, three braces.

When you save, the editor checks what you typed:

  • A subject longer than one line or 998 bytes is not saved.
  • A variable that belongs to headers, footers and layouts, used in an email’s body, stays as typed. The editor asks first, and Save anyway saves it.
  • A variable this template doesn’t know is saved with a warning, and the template shows Unknown placeholders: with the names.

When someone else changed the file

If the file changed on disk after you opened it (edited by hand, by a git pull or by another user), the save is refused. A box shows what is on disk now and your text stays in the form. Reload to work on the current version.

Layout, preview and test email

  • Layout (for an email): choose which layout wraps this email. The first choice is the default layout. The preview follows at once.
  • What is sent (for an email PhoenixKit sends): for the subject, text, HTML and Markdown, where each comes from: this file, another file, an empty file (the part is left out), the built-in text, or the Emails module’s database template, which wins over every file. For an email your app sends itself it says Sent by the application itself: these files are what it sends.
  • Preview shows the email with sample values, in its layout, with HTML and Text tabs. A new template says Save the template to preview it. The preview runs no scripts and loads images only from your own site.
  • Send to me sends this email, with the sample values, to your own address and nobody else. You can send one every 30 seconds. It works on read-only servers too.

New templates and deleting

New template at the bottom of the list creates a template. Type a name and pick Empty or Copy of … another template, then Create. The name must start with one of the prefixes the host allows, and must not exist yet. An empty template is marked not saved yet until you save its first field. A copy takes every file of the original. If the original has extra files of your own, they are listed under Also copy the host’s own files:, ticked; untick any to leave out.

Delete on an open template asks Delete “…” and all its files? and deletes it on Delete.

Every save, deletion, copy and layout change is written to PhoenixKit’s activity log, with who did it and a short diff.

What it doesn’t do

  • It comes with no templates of its own. They are your app’s files.
  • It doesn’t write files in production unless the host turns editing on there.
  • It doesn’t commit to git. Committing and deploying the changed files is up to you.
  • It doesn’t let anyone in who couldn’t already open the email settings page: its permission can only narrow that.
  • It doesn’t read a template file that is a link, leads outside the templates folder or is larger than 256 KiB; such a file shows as unavailable.

For developers

The module needs a block of config before it shows anything: at least name_prefixes (which templates the editor sees; none by default) and a templates root that exists. Editing is allowed only where config :phoenix_kit_templates_editor, editable: true, typically config/dev.exs. For saves to know who is saving, add the scope hook:

config :phoenix_kit,
  extra_live_session_on_mount: [PhoenixKitTemplatesEditor.Web.ScopeHook]

Clicking a variable inserts it at the cursor only when mix.exs lists the :phoenix_kit_js_sources compiler; without it the variable goes at the end of the body. The package README lists every config key and callback (preview, sample_variables, test_send and others).