Skip to content

fix(mailgun): refuse deleted Mailgun templates before sending; no suppression for ESP blocks - #29

Merged
Schero94 merged 2 commits into
mainfrom
fix/mailgun-template-preflight
Nov 19, 2026
Merged

Schero94 merged 2 commits into
mainfrom
fix/mailgun-template-preflight

Conversation

@Schero94

Copy link
Copy Markdown
Owner

Summary

Two Mailgun fixes found and proven against the real Mailgun API (US sandbox domain, 2026-11-19). Full record: docs/acceptance/2026-11-19-mailgun-live.md.

1. Template sends: refuse a template that is gone at Mailgun (fix(mailgun))

Mailgun answers 200 Queued for a send that names a template it does not have, then drops the message (event failed, severity: temporary, delivery-status 621 "Template not found", retried every 600 s). 4.9.0 logged such mails as Sent.

sendViaMailgun (template branch) now calls checkMailgunTemplate (server/src/utils/provider-api.js, reuses getJson, 5 s timeout, fixed hosts):

  • domain scope: GET /v3/{domain}/templates/{name}?active=yes; account scope: GET /v4/templates/{name}?active=yes (the account-level get also takes the template name, per the Mailgun OpenAPI bundle)
  • 404 or 200 without template.version (live: that is what Mailgun returns for a template without a version) → MAGIC_MAIL_PROVIDER_TEMPLATE_UNAVAILABLE before any messages request; tagged pre-dispatch (another account with the template may send) and fallback-safe (the configured local fallback template applies), log row failed
  • network / timeout / 401 / 403 / 429 / 5xx of the check → warning (code only), send goes ahead
  • positive results cached 60 s per account + region + domain + scope + name (max 1000 entries); parallel sends share one in-flight check; negatives are never cached

2. Webhook: Mailgun espblock and soft bounces no longer suppress the recipient (fix(webhooks))

Task check result: failed + temporary → Deferred, never suppressed (correct already, now also tested with the live 621 shape). failed + permanent → Bounced + suppressed: wrong for one live case. A real send was refused by the recipient's provider: permanent, reason espblock, 550 5.7.40, bounce-type: soft; Mailgun did not put the address on its own bounce list (GET /v3/{domain}/bounces/<addr> → 404). 4.9.0 would have suppressed the address on every account. Now:

  • espblock → Blocked, never suppressed
  • permanent with bounce-type: soft → Bounced, not suppressed
  • code 621 → MAILGUN_TEMPLATE_NOT_FOUND (Deferred while temporary, Failed when permanent), never suppressed
  • permanent hard bounces still suppress; suppress-* and old unchanged

Live results

Run Result
LIVE_PROVIDERS=mailgun npm run test:live on main 4.9.0 14/15 Mailgun tests pass (ghost-template assertion got 200 Queued)
same on this branch 63 tests, 15 passed, 0 failed, 48 skipped (other providers/EU not selected). Ghost template → MAGIC_MAIL_PROVIDER_TEMPLATE_UNAVAILABLE, no messages request, real check answered 404

Webhook end to end (Strapi 5.63.0 + this branch's build, SQLite, filter proxy, cloudflared quick tunnel, domain webhooks delivered / permanent_fail / temporary_fail registered via POST /v3/domains/{domain}/webhooks):

  • unsigned → 401; other paths → 404 (proxy)
  • real signed temporary_fail (621) → 200, signature verified with the real signing key, log Deferred, no suppression; its Mailgun retry 10 min later, on the rebuilt host → 200, log reason MAILGUN_TEMPLATE_NOT_FOUND
  • real signed delivered (test mode, code 650) → 200, deliveryConfirmedAt set
  • replay unchanged → 200, nothing processed twice; tampered signature → 401; tampered token → 401; signature removed → 401
  • fixed send path with a deleted template → refused, no message sent, log failed
  • Before/after: GET /v3/domains/{domain}/webhooks and GET /v1/webhooks saved before the run; after deleting the three webhooks both responses are byte-identical. Tunnel and host stopped, host .env/DB deleted. Domain templates unchanged.
  • Real (non-test-mode) sends: 2, subjects "Magic Mail live test (can be deleted)" (deleted-template send, not delivered; one template send, refused by the recipient's provider with espblock).

Not proven live (spec/test only)

  • permanent_fail webhook delivery: Mailgun recorded the espblock failure but no permanent_fail post arrived within 10 min (temporary and delivered posts did). Mapping covered by tests with the observed shape. A hard bounce cannot be produced on a sandbox domain.
  • Final event after Mailgun stops retrying a 621 (expected reason old).
  • Account-level template 200 path (account has none; live 404 only), EU region.
  • The first two test-mode sends ~15 s after creating the webhooks produced no webhook; all later ones did.

Checks

  • npm run lint, npm run format:check: clean
  • npm test: 927 tests, 924 pass, 0 fail, 3 skipped (PostgreSQL multi-process tests without MAGIC_MAIL_TEST_PG_URL, as before); npm run test:admin: 20/20
  • npm run build, npm run verify, npm run verify:runtime: pass; npm audit --omit=dev: 0 vulnerabilities; no dependency added
  • New tests: tests/mailgun-template-check.test.js (11, fake Mailgun server: missing, no active version, cache TTL, negatives not cached, in-flight sharing, /v4 account scope, EU host, check failures do not block, fallback template, failover to another account, no check for plain sends); tests/webhook-outcomes.test.js "Mailgun severity and bounce type" (fails on 4.9.0)
  • Docs: docs/delivery.md (mapping table and suppression rule), docs/sending.md, docs/live-webhook-checks.md (test mode does emit delivered webhooks), docs/acceptance/2026-11-19-mailgun-live.md, CHANGELOG [Unreleased] (Fixed + Migration)

Release type: two fix: commits → patch.

🤖 Generated with Claude Code

Schero94 and others added 2 commits November 19, 2026 22:44
Mailgun answers 200 "Queued" for a send that names a template it does
not have and drops the message later (event failed, severity temporary,
delivery-status 621 "Template not found"; verified live 2026-11-19), so
Magic Mail logged such mails as sent.

sendViaMailgun now checks the template before a template send:
GET /v3/{domain}/templates/{name}?active=yes (domain scope) or
GET /v4/templates/{name}?active=yes (account scope). A 404 or a template
without an active version throws MAGIC_MAIL_PROVIDER_TEMPLATE_UNAVAILABLE
before any message request (pre-dispatch, fallback-safe). Positive results
are cached for 60 s per account and template; parallel sends share one
check. Network, timeout, auth, 429 and 5xx failures of the check only log
a warning and the send goes ahead.

Docs read: Mailgun templates API reference and its OpenAPI bundle
(documentation.mailgun.com/docs/mailgun/api-reference/send/mailgun/templates,
retrieved 2026-11-19), plus live answers of the US API for unknown and
version-less templates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…soft bounces

A permanent Mailgun failure with reason espblock (seen live: 550 5.7.40,
bounce-type soft; Mailgun did not add the address to its bounce list) was
mapped to a hard bounce and suppressed the recipient. It is now recorded
as blocked and never suppressed; permanent failures with bounce-type soft
stay bounced but are not suppressed; code 621 (template not found) gets
MAILGUN_TEMPLATE_NOT_FOUND and never suppresses. Temporary failures were
already deferred without suppression.

Also records the Mailgun live run (send path, live suite, webhook end to
end through a cloudflared tunnel) and the CHANGELOG entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Schero94
Schero94 merged commit 4ac0ba3 into main Nov 19, 2026
8 checks passed
@Schero94
Schero94 deleted the fix/mailgun-template-preflight branch November 19, 2026 21:49
github-actions Bot pushed a commit that referenced this pull request Nov 19, 2026
## [4.9.1](v4.9.0...v4.9.1) (2026-11-19)

### Bug Fixes

* **mailgun:** refuse deleted Mailgun templates before sending; no suppression for ESP blocks ([#29](#29)) ([4ac0ba3](4ac0ba3))

### Fixed

* **Mailgun: a template deleted at Mailgun is no longer logged as sent.** Mailgun accepts a send that names a template it does not have (200 "Queued") and drops the message afterwards (event `failed`, delivery-status 621 "Template not found", retried every 10 minutes). Before a template send MagicMail now checks the template at Mailgun (`GET /v3/{domain}/templates/{name}?active=yes`, account-level templates `GET /v4/templates/{name}?active=yes`). A deleted template, or one without an active version, fails with `MAGIC_MAIL_PROVIDER_TEMPLATE_UNAVAILABLE` before any message is sent: another account that still has the template sends instead, else the configured Magic Mail fallback template, else the error is returned and the delivery log row is `failed`. A successful check is cached for 60 seconds per account and template (one extra request per template and minute for bulk sends, parallel sends share one check). If the check itself fails (network, timeout, 401/403, 429, 5xx), the send goes ahead and a warning is logged.
* **Mailgun webhooks: ESP blocks and soft bounces no longer suppress the recipient.** A permanent failure with reason `espblock` (the recipient's provider refused the sender, for example `550 5.7.40`) is now recorded as **Blocked** and never added to the suppression list; Mailgun itself does not put such addresses on its bounce list. A permanent failure that Mailgun classifies as `bounce-type: soft` is still recorded as **Bounced** but not suppressed. Only permanent hard bounces suppress, as before. Template-not-found failures (code 621) get the reason code `MAILGUN_TEMPLATE_NOT_FOUND` (Deferred while Mailgun retries, Failed when final) and never suppress.

### Migration

* Nothing to do. Mailgun template sends make one extra read request per template and minute; the API key needs read access to templates, which provider-template sync already requires. Addresses suppressed earlier because of a Mailgun `espblock` can be removed under **Plugin Settings → Bounces and recipient checks → Suppressed recipients** (source `mailgun`, reason code `MAILGUN_ESPBLOCK_*`).
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