Skip to content

feat: provider templates for SendGrid and Mailgun - #20

Merged
Schero94 merged 1 commit into
mainfrom
feat/provider-templates
Nov 18, 2026
Merged

Schero94 merged 1 commit into
mainfrom
feat/provider-templates

Conversation

@Schero94

@Schero94 Schero94 commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Provider templates (SendGrid, Mailgun) — minor release (the minor after #23)

Stacked on #23. This PR targets fix/provider-send-paths (#23) and reuses its shared send-path helpers. GitHub retargets it to main when #23 is merged, so the two can merge back-to-back.

Not mergeable yet: the live suites against the real SendGrid and Mailgun APIs (npm run test:live) have not run. The maintainer test accounts (~/.config/magic-mail-live/.env) still do not exist (checked 2026-11-18). This PR stays a draft until a passing live run is recorded in docs/acceptance/2026-11-17-provider-templates.md. Merge order: #23 first, then this PR.

Implements the P1 plan (Strapi RFC #27752): SendGrid dynamic templates and Mailgun templates are synced read-only and listed next to the local templates. MagicMail can preview them, send tests, and send them with the same data object that local templates render. Routing, failover, quotas and logs stay in MagicMail.

What changes for users

  • Templates screen. With a SendGrid or Mailgun account, a Provider templates section appears under the local templates. Each row shows the provider badge, the key (sendgrid:welcome-email), the date the template last changed at the provider, the accounts that have it and its status. The list syncs itself when it is older than 10 minutes, or on Sync. Without such an account nothing new is visible.
  • Template dialog. A sandboxed preview, the detected variables, a code example, an optional fallback template, a Send test (to your own admin address by default, with the same sample values local tests use) and Copy as Magic Mail template. The copy turns Handlebars blocks into Mustache and confirms first when something does not carry over.
  • Core emails. Password reset and email confirmation get a Design choice: the Magic Mail designer (default) or a provider template. The dialog shows the v1 payload variables and warns about variables MagicMail does not send. An optional fallback to the designer version is available. A red notice on the tab and on the dashboard appears when the chosen template is unavailable.
  • Code snippets dialog. It gains Convert for SendGrid or Mailgun (Mustache to Handlebars).
  • SendGrid region. Comes from feat(providers): SendGrid EU region and safer SendGrid/Mailgun send paths #23 (Global or EU, api.eu.sendgrid.com); template sync uses the same region helper, so EU accounts sync through the EU host.

Behaviour

  • Same template data. send({ to, providerTemplate, data }) sends the same data as SendGrid dynamic_template_data or Mailgun t:variables. The data is converted to plain JSON (ISO dates; no functions, cycles or prototype keys).
  • Size limits. feat(providers): SendGrid EU region and safer SendGrid/Mailgun send paths #23's Mailgun 16 KB check runs once on every send and counts template data together with headers and the v:magicmail_*, o:tag and tracking options; a template send above it fails before any request with MAGIC_MAIL_PROVIDER_TEMPLATE_TOO_LARGE (other sends keep MAGIC_MAIL_MESSAGE_TOO_LARGE). The same applies to feat(providers): SendGrid EU region and safer SendGrid/Mailgun send paths #23's SendGrid 30 MB check. Template data is capped at 1 MB. Headers are never dropped or shortened.
  • Routing. Only accounts that have the template are used, and failover happens only between those accounts. An explicit account without the template is an error.
  • Strict errors. A missing or unpublished template is a clear error. A local fallback is opt-in per template and is used only when no provider accepted the message; an ambiguous provider error never triggers it.
  • Core emails. users-permissions renders its core emails itself. MagicMail records the variables of that render, keyed by the rendered body (bounded, 5 min TTL, allowlisted USER). It then sends payload v1: the familiar names plus url, baseUrl, user, token/code and meta.
  • Not accepted everywhere. The public Content API does not accept providerTemplate, and provider-template mails are not queued in the outbox in this release.

Old features

Stacked on #23 (rebase of 2026-11-18)

Fixture Added by #23 Why
local-sendgrid.json custom_args.magicmail_id; tracking_settings.click_tracking/open_tracking.enable=false delivery-log id for webhooks; Magic Mail applied pixel and link tracking, so SendGrid's is off
plain-sendgrid.json, core-reset-sendgrid.json custom_args.magicmail_id; tracking_settings.open_tracking.enable=false pixel applied, no links rewritten
local-sendgrid-untracked.json custom_args.magicmail_id tracking off, so no tracking switches
local-mailgun.json o:tracking-opens=no, o:tracking-clicks=no, v:magicmail_email_type, v:magicmail_priority, v:magicmail_id, o:tag tracking switches for both tracked parts plus metadata
core-reset-mailgun.json o:tracking-opens=no, v:magicmail_email_type, v:magicmail_priority, v:magicmail_id, o:tag pixel only
local-mailgun-untracked.json v:magicmail_email_type (marketing), v:magicmail_priority (high), v:magicmail_id, o:tag (marketing) tracking off
local-smtp.json, core-reset-smtp.json none unchanged

The fake delivery logs in the golden and provider-template harnesses now return alphanumeric documentIds like Strapi (golddoc1, logdoc1 instead of log-1). #23 sends only ids matching /^[A-Za-z0-9]{1,64}$/, so the old fake id silently suppressed magicmail_id.

  • New test. Template sends carry custom_args.magicmail_id / v:magicmail_* / o:tag and no provider tracking switches; template data that fits 16 KB alone but not with the metadata is refused before any request, exactly 16 KB is accepted.
  • One live command. npm run test:live = node --test --test-concurrency=1 tests/live/*.live.test.js runs tests/live/provider-send.live.test.js (feat(providers): SendGrid EU region and safer SendGrid/Mailgun send paths #23) and tests/live/provider-templates.live.test.js. Credentials only from ~/.config/magic-mail-live/.env, never printed. Required: SENDGRID_API_KEY, SENDGRID_FROM, SENDGRID_TEMPLATE_ID, MAILGUN_API_KEY, MAILGUN_DOMAIN, MAILGUN_REGION, MAILGUN_TEMPLATE, LIVE_TEST_RECIPIENT; optional SENDGRID_REGION, SENDGRID_EU_API_KEY, SENDGRID_EU_FROM, MAILGUN_FROM, MAILGUN_EU_DOMAIN, MAILGUN_EU_API_KEY. Without the file: the send suite skips all 15 tests with the path, the template suite fails test 1 with the path (10 skipped), exit 1.
  • Browser smoke findings, fixed here. Provider-template previews showed black text on a dark canvas in dark mode (now a light email canvas in both themes). The account wizard logged Radix's "Missing Description" warning on open (pre-existing on main; now aria-describedby={undefined} like the other plugin dialogs; checked against the installed @radix-ui/react-dialog 1.1.5 source).
  • Gate on fb24a7f (Node 22.23.1): lint, format check, npm test (693 tests, 690 pass, 3 skipped PostgreSQL), test:multi-instance (49/46, 3 skipped PostgreSQL), build, verify, verify:runtime, npm ci --ignore-scripts with npm 10.9.8 and 11.21.2.
  • Browser smoke (isolated Strapi 5.62.0, packed tarball, SQLite, egress blocked, fake SendGrid/Mailgun): wizard SendGrid region EU saved as eu; Mailgun domain mg.example.test/v3 shows the inline error, https://MG.example.test/ is saved as mg.example.test; sync (EU account via api.eu.sendgrid.com), preview, test sends through SendGrid US/EU and Mailgun with feat(providers): SendGrid EU region and safer SendGrid/Mailgun send paths #23's metadata, copy, fallback, core-email design. 1440 and 390, light and dark: document overflow 0, nothing outside the viewport, 0 plugin console messages. Host stopped and secrets deleted.

Evidence

  • Tests. Unit, integration (fake SendGrid and Mailgun servers) and contract tests. The contract tests validate every request and every fake response against the official OpenAPI documents, vendored and pinned by SHA-256:
    • twilio/sendgrid-oai at 51a0dee
    • the Mailgun API reference bundle
    • Three spec-versus-documentation deviations are listed with reasons and re-checked by the live suite.
  • Acceptance. Browser acceptance ran on an isolated Strapi 5.62.0 host from the packed tarball. It used SQLite and PostgreSQL, light and dark, widths 1440/768/390/320, five locales, a read-only role, error and empty states, and egress blocked except the fake providers. The results and click-path table are in docs/acceptance/2026-11-17-provider-templates.md.
  • Unauthenticated probes. Probes against the real endpoints, without credentials, returned 401 on every documented path for US and EU. This proves only that the hosts are reachable. For Mailgun it also weakly confirms the paths, because unknown paths return 404 there.

Docs read (docs-first, checked 2026-11-17)

Release

🤖 Generated with Claude Code

@Schero94
Schero94 force-pushed the feat/provider-templates branch from a683ab6 to 1c2f87b Compare November 18, 2026 14:31
@Schero94
Schero94 changed the base branch from main to fix/provider-send-paths November 18, 2026 14:32
@Schero94
Schero94 force-pushed the feat/provider-templates branch 2 times, most recently from c3ac91e to 2363e3e Compare November 18, 2026 21:53
@Schero94
Schero94 force-pushed the feat/provider-templates branch from 2363e3e to 882a86a Compare November 18, 2026 22:19
@Schero94
Schero94 changed the base branch from fix/provider-send-paths to main November 18, 2026 23:01
Schero94 added a commit that referenced this pull request Nov 18, 2026
…ceptance after the rebase onto #20

The SendGrid/Mailgun live suites accept brevo and ses in LIVE_PROVIDERS and
skip instead of failing; the Brevo and SES suites honour the selection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Re-applies #20's own changes (0f3b79f..882a86a) on top of 4.7.0, which
already contains #23. Conflicts resolved in favour of main: the 4.6.3
dialog labelling, the 4.7.0 send paths with the Mail Send hint and the
Strapi 5.63 dev dependencies. The CHANGELOG [Unreleased] section holds
only the provider-template entries plus a Verification section.

Adds the provider-template screens to the StrictMode admin tests: the
templates page loads the overview once, the preview and core-email
design dialogs open and close cleanly, and a stale list starts exactly
one background sync under StrictMode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Schero94
Schero94 force-pushed the feat/provider-templates branch from 882a86a to 936a874 Compare November 18, 2026 23:08
@Schero94
Schero94 marked this pull request as ready for review November 18, 2026 23:08
@Schero94
Schero94 merged commit 184115d into main Nov 18, 2026
8 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 18, 2026
# [4.8.0](v4.7.0...v4.8.0) (2026-11-18)

### Features

* provider templates for SendGrid and Mailgun ([#20](#20)) ([184115d](184115d))

### Added

* **provider templates:** SendGrid dynamic templates and Mailgun templates (domain and account level) appear read-only under **Email Templates → Provider templates** as soon as a SendGrid or Mailgun account exists. Sync reads them with the stored account key (never writes to the provider), follows the providers' pagination, uses the EU hosts for EU accounts, and refreshes itself when the list is older than 10 minutes. A template whose remote id disappears but whose name still exists on the account (a database moved to another environment) is matched by name.
* **provider templates:** preview dialog with a sandboxed preview, the detected variables, a code example, a test send (to your own admin address by default, with the same sample values as local templates), an optional local fallback template, and **Copy as Magic Mail template** (HTML copy; Handlebars blocks become Mustache sections and anything that does not carry over is listed).
* **sending:** `send({ to, providerTemplate: '<provider>:<slug>', data })` sends a synced template. The same `data` object that local templates render with Mustache is sent as SendGrid `dynamic_template_data` or Mailgun `t:variables`, converted to plain JSON (dates as ISO strings, no functions, no cycles) and checked against the provider limits (Mailgun: 16 KB for all send options; Magic Mail bounds template data to 1 MB). Routing, rules, failover, quotas and logs work as before, but only between accounts that have the template. Strict by default: a missing template or a provider refusal is a clear error; a local fallback template is sent only when it is configured and no provider accepted the message. The log stores the template key and variable names, never the values.
* **core emails:** password reset and email confirmation can use a provider template (**Core emails → Design**). Magic Mail sends the familiar users-permissions names (`URL`, `TOKEN`/`CODE`, `USER`, `SERVER_URL`, `ADMIN_URL`) plus `url` (the finished link), `baseUrl`, `serverUrl`, `adminUrl`, `user`, `token`/`code` and `meta` (`templateKey`, `payloadVersion: 1`, `coreEmailType`). `USER` contains only `id`, `email`, `username`, `firstname`, `lastname`. Optional fallback to the Magic Mail designer version. A red notice on the Core emails tab and the dashboard names core emails whose template is not available.
* **templates:** **Convert for SendGrid or Mailgun** in the code snippets dialog turns a local template's Mustache logic into Handlebars for upload at the provider.
* **admin API:** `GET /magic-mail/provider-templates`, `POST /magic-mail/provider-templates/sync`, `GET /magic-mail/provider-templates/:key`, `POST /magic-mail/provider-templates/:key/test-send`, `PUT /magic-mail/provider-templates/:key/settings`, `POST /magic-mail/provider-templates/:key/copy`, `GET /magic-mail/designer/templates/:id/provider-html`, `GET /magic-mail/designer/core-designs`, `PUT /magic-mail/designer/core/:coreEmailType/design`. Existing permissions: `read` (sync is read-only at the provider and rate limited), `test-send`, `manage`; the core-email routes also need the users-permissions email-template permissions.

### Security

* Provider ids are never accepted from callers, only synced keys. The public Content API send routes do not accept `providerTemplate`.
* Template previews render in a sandboxed iframe without scripts. Provider requests go only to the fixed SendGrid and Mailgun hosts, with timeouts and response size limits; paging URLs from the provider are never requested directly.

### Migration

* Provider templates: nothing to do. A new collection `magic_mail_provider_templates` is created on start; nothing changes for accounts without SendGrid or Mailgun. Sends without a provider template are byte-identical to 4.7.0 (golden payload tests). Provider-template sends are not queued in the outbox in this release.
* To sync SendGrid templates, the SendGrid key also needs **Template Engine → Read Access** (sending alone needs only **Mail Send**).

### Verification

* SendGrid: tested live against the real SendGrid API on 2026-11-18, every send in sandbox mode: template sync with pagination, variable detection and preview, template sends, unknown and malformed template ids, the 1 MB data bound, and the template life cycle with temporary templates (no version, a first and a second active version, deleted and re-created under the same name, deleted for good). Temporary objects were deleted afterwards. The EU host could not be tested live (no EU subuser).
* Mailgun: verified against Mailgun's official OpenAPI specification and with tests, but not yet against the live Mailgun API. Please report anything unexpected.
Schero94 added a commit that referenced this pull request Nov 18, 2026
…open/click tracking

Re-landed on main after #20 was squash-merged: the changes of #28 (diff from
its #20 base 882a86a) applied onto origin/main, CHANGELOG [Unreleased]
merged with #20's entries, StrictMode admin tests added for the Brevo/SES
credentials steps and connection check, the Brevo template dialog with
language versions and the provider-tracking switch.

- Brevo: send, Check connection, webhook token (reveal/rotate, audited),
  templates with params; Amazon SES v2: own SigV4 (38 official vectors),
  send, templates, Check connection, SNS-verified events.
- Language versions of provider templates (exact -> language -> default).
- Provider open/click events counted for provider-template mails.
- Brevo and Amazon SES are labelled Beta in the wizard until a live run
  against their APIs passes (see CHANGELOG Verification).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Schero94 added a commit that referenced this pull request Nov 19, 2026
…open/click tracking

Re-landed on main after #20 was squash-merged: the changes of #28 (diff from
its #20 base 882a86a) applied onto origin/main, CHANGELOG [Unreleased]
merged with #20's entries, StrictMode admin tests added for the Brevo/SES
credentials steps and connection check, the Brevo template dialog with
language versions and the provider-tracking switch.

- Brevo: send, Check connection, webhook token (reveal/rotate, audited),
  templates with params; Amazon SES v2: own SigV4 (38 official vectors),
  send, templates, Check connection, SNS-verified events.
- Language versions of provider templates (exact -> language -> default).
- Provider open/click events counted for provider-template mails.
- Brevo and Amazon SES are labelled Beta in the wizard until a live run
  against their APIs passes (see CHANGELOG Verification).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant