phil / email-templates-bundle
Let sites edit their emails safely: templates per site and locale, with versions, sandboxed Twig, email-safe components and a preview.
Package info
github.com/parler-haut-interagir-librement/email-templates-bundle
Type:symfony-bundle
pkg:composer/phil/email-templates-bundle
Requires
- php: >=8.5
- ext-dom: *
- ext-filter: *
- ext-libxml: *
- ext-mbstring: *
- ext-sodium: *
- doctrine/dbal: ^4.6
- doctrine/orm: ^3.8
- phil/content-bundle: ^0.3@dev
- phil/email-address: ^0.1@dev
- phil/locale-dimensions: ^0.1@dev
- phil/markup: ^0.3@dev
- phil/url: ^0.1@dev
- psr/clock: ^1.0
- psr/event-dispatcher: ^1.0
- symfony/config: ^8.2
- symfony/dependency-injection: ^8.2
- symfony/mime: ^8.2
- symfony/service-contracts: ^3.8
- symfony/uid: ^8.2
- symfony/yaml: ^8.2
- twig/twig: ^3.31.1
Requires (Dev)
- ext-gd: *
- doctrine/doctrine-bundle: ^3.4
- doctrine/persistence: ^4.2
- phil/audit-bundle: ^0.1@dev
- phil/tenancy-bundle: ^0.2@dev
- phil/ux-kit: >=0.5.0-alpha <1.0
- phpunit/phpunit: ^13.4
- psr/log: ^3.0
- symfony/browser-kit: ^8.2
- symfony/clock: ^8.2
- symfony/console: ^8.2
- symfony/css-selector: ^8.2
- symfony/dom-crawler: ^8.2
- symfony/event-dispatcher: ^8.2
- symfony/framework-bundle: ^8.2
- symfony/http-foundation: ^8.2
- symfony/http-kernel: ^8.2
- symfony/mailer: ^8.2
- symfony/messenger: ^8.2
- symfony/routing: ^8.2
- symfony/security-bundle: ^8.2
- symfony/security-core: ^8.2
- symfony/security-csrf: ^8.2
- symfony/translation: ^8.2
- symfony/twig-bundle: ^8.2
- symfony/ux-toolkit: ^3.8
- symfony/ux-twig-component: ^3.8
- twig/cssinliner-extra: ^3.26
- twig/html-extra: ^3.31.1
- twig/intl-extra: ^3.31
Suggests
- phil/audit-bundle: To record each change of a template in the audit trail
- phil/tenancy-bundle: To keep the templates per tenant, the current site being the active tenant
- phil/ux-kit: For the recipes the administration draws, through the Symfony UX Toolkit
- symfony/console: For phil:email-templates:list, check and render
- symfony/mailer: To send a template from a message, through SendTemplatedEmail
- symfony/security-csrf: For the administration (admin.enabled), with symfony/security-core and symfony/ux-twig-component
- symfony/twig-bundle: To render the house layout with the Twig of the application, and override it
- twig/cssinliner-extra: To inline the styles of the layout into each element, for the clients that read no style sheet
- twig/intl-extra: For format_date, format_number and format_currency in the templates
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-28 03:39:16 UTC
README
Email templates that several sites share and each site may change — a confirmation, a ticket, a cancellation —,
written in sandboxed Twig with email-safe components, drafted, published and versioned per site and locale, previewed,
and rendered into a Symfony Mailer Email.
phil/email-templates-bundle renders the emails an application sends from templates that people other than its
developers may edit. A template has a subject, a preheader, a body in HTML or in Markdown, an optional
hand-written plain-text part, the schema of its variables — named slots with their types, whether they are
required, their defaults and sample values — and the segments its conditional blocks test. The defaults ship as
YAML files, the bundle's and the application's, the application's winning. Each site, or the scope every site
shares, keeps per locale a draft of its own and the versions it published, as contents of phil/content-bundle:
a draft is sent only once published, a published version is never changed, and the latest published one is sent. While a
site publishes nothing in a locale, the shared template or the default file is sent. A locale falls back to its
language, then to the default locale.
The sources render in a Twig sandbox of their own, in Twig's strict mode: an allow-list of tags, filters,
functions, tests and methods, nothing included, nothing of the application's Twig in reach, loops nested at most two
deep, an output ceiling. The components — button(), table(), image(), panel, footer and segment() —
draw tables with inline styles that the email clients render alike. A Markdown body is read by phil/markup and
purified, its components kept whole. The house layout, a template of the application's Twig, wraps every email:
the hidden preheader, the brand, the body, the colours of a light and a dark scheme through color-scheme and
prefers-color-scheme; the styles are then inlined by twig/cssinliner-extra, and the plain-text part is written from
the HTML unless the template writes its own. Images are embedded by content identifier, cid:logo.
Every value given to a render is checked against the schema; every template is checked when it is saved and
when it is published — its
sources compile in the sandbox, read only what the schema declares, and render in every variant with the sample
values. The result is a RenderedEmail — subject, preheader, HTML, text, images — or an Email ready to send; a
message, SendTemplatedEmail, carries identifiers only and sends it through Messenger. An administration for a
configurable role edits, publishes, reverts, discards and takes offline the templates of a site, and previews the
draft in each variant — no segment, each segment, all of them — in an <iframe sandbox> served with a policy that runs
no script; its pages are drawn by the recipes of the PHIL UX kit or of the Shadcn kit, in English and French.
It is a Symfony bundle on Twig, phil/content-bundle and the Doctrine ORM, phil/markup, phil/url,
phil/email-address and Symfony Mime.
phil/tenancy-bundle makes the active tenant the site; phil/audit-bundle records every change in its trail; the
console, Mailer, Messenger, Security, the CSRF protection, Symfony UX Twig Component, twig/cssinliner-extra and
twig/intl-extra are optional. The templates live in two tables beside those of the contents, on SQLite, MySQL, MariaDB
or PostgreSQL, their sites and keys compared byte for byte, tested on MySQL 8.4 and PostgreSQL 17; a store in memory
serves tests. Its services keep no state between requests and serve a long-running worker.
Status
| Part | State |
|---|---|
TemplateKey, Site, Locale, TemplateDefinition, TemplateDraft, TemplateVersion, BodyFormat, TemplateOrigin, PublicationState, the exceptions and the events |
Implemented and tested |
VariableSchema, Variable, VariableType: named slots, nine types, defaults, samples, limits, values checked and normalized |
Implemented and tested |
TemplateResolver: the site's published version, the shared one, the default files, locale by locale; the draft of a preview |
Implemented and tested |
SandboxedEngine, SandboxRules, LimitsVisitor, SourceLoader: the strict Twig sandbox, its checks and its cache |
Implemented and tested |
EmailComponents: button, table, image, panel, footer, segment, in HTML, Markdown and text |
Implemented and tested |
EmailRenderer, HtmlToText, TwigLayout, CssInlinerStyles, DirectoryEmbedLoader, MarkupMarkdown: render without sending |
Implemented and tested |
EmailComposer, SendTemplatedEmail and its handler: an Email ready to send, a message with identifiers only |
Implemented and tested |
EmailTemplates, TemplateValidator: save, publish, revert, discard, reset, history, catalog of a site |
Implemented and tested |
ContentTemplateStore, EmailTemplateContent, EmailTemplateDimension, TemplatePayload, TemplateKeysCollation, InMemoryTemplateStore, YamlDefaultTemplates |
Implemented and tested |
PreviewRenderer, SamplePreviews, AdminController and its pages, PHIL UX and Shadcn |
Implemented and tested |
TenancySiteScope, AuditTemplateChanges; the commands list, check and render; the Flex recipe |
Implemented and tested |
Documentation
| Document | What it covers |
|---|---|
| Installation | Requirements, installing, the contents and their locales, templates kept in a table of versions, the recipes of the kit. |
| Configuration | Every option, with an example. |
| Templates | The default files, the fields of a template, the variable schema, segments and images. |
| The sandbox and the components | What a template may write, the components, Markdown, the limits. |
| Rendering and sending | Finding a template, the layout, the dark mode, the inlined styles, the plain text, Mailer and Messenger. |
| Sites, drafts and versions | The sites, the shared scope, drafts, publication and versions, the contents, tenancy and audit. |
| Administration and preview | The pages, the preview frames, the role, the CSRF token, the kit. |
| Console | phil:email-templates:list, check and render. |
| Security | What the sandbox, the checks and the preview protect against, and what is left to the application. |
| Extension points | The services to replace, the tags to add to, and when they are stable. |
| Testing | The bundle's tests, the servers its integration tests read, the fuzz target and the benchmark. |
| Reference | Every class and service of the bundle. |
Quick start
// config/bundles.php Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true], Phil\ContentBundle\PhilContentBundle::class => ['all' => true], Phil\EmailTemplatesBundle\PhilEmailTemplatesBundle::class => ['all' => true],
# config/packages/phil_content.yaml phil_content: locales: { en: [], fr: [] } # config/packages/phil_email_templates.yaml phil_email_templates: from: 'Shop <shop@example.com>' brand: { name: 'Shop' }
use Phil\EmailTemplatesBundle\Application\EmailComposer; use Phil\EmailTemplatesBundle\Application\Rendering\RenderOptions; $email = $composer->compose('ticket', [ 'recipient' => ['name' => 'Ada'], 'event' => ['title' => 'Autumn concert', 'starts_at' => '2026-10-03T20:00:00-04:00'], 'ticket' => ['number' => 'T-7781'], 'ticket_url' => 'https://example.com/tickets/T-7781', ], new RenderOptions(locale: 'fr')); $mailer->send($email->to('ada@example.com'));
Requirements
PHP 8.5 or later, Symfony 8.2 or later, Twig 3.31 or later, and phil/content-bundle with DoctrineBundle for the
templates the sites write.
Contributing
Contributions are welcome on the PHIL monorepo: read the contribution guide and the code of conduct. Report a vulnerability privately, as the security policy explains, never in a public issue.
License
MIT, © Philippe Gamache, doing business as Le Gnome Archiviste; a Parler Haut Interagir Librement project. See LICENSE.