Skip to content

feat(inbound): InboundEmailService orchestrates reply/create with tests#46

Merged
mpge merged 5 commits intomasterfrom
feat/inbound-email-orchestration
Apr 27, 2026
Merged

feat(inbound): InboundEmailService orchestrates reply/create with tests#46
mpge merged 5 commits intomasterfrom
feat/inbound-email-orchestration

Conversation

@mpge
Copy link
Copy Markdown
Member

@mpge mpge commented Apr 27, 2026

Recovers auto-closed #33. Rebased onto current master.

mpge added 4 commits April 26, 2026 22:12
…Router

Greenfield inbound-email foundation for Symfony. Transport-agnostic
InboundMessage + InboundAttachment DTOs, InboundEmailParser
interface, and InboundRouter that resolves an inbound email to an
existing ticket via canonical Message-ID parsing + signed Reply-To
verification.

Completes the 5-framework greenfield inbound set. Mirrors the NestJS
reference, the 5 per-framework inbound-verify PRs (Laravel, Rails,
Django, Adonis, WordPress), and the .NET / Spring / Go / Phoenix
greenfield routers.

Resolution order (first match wins):
  1. In-Reply-To parsed via MessageIdUtil — cold-start path.
  2. References parsed via MessageIdUtil, each id in order.
  3. Signed Reply-To on toEmail verified via MessageIdUtil. Survives
     clients that strip threading headers; forged signatures are
     rejected via hash_equals (timing-safe).
  4. Subject-line reference tag [{PREFIX}-...].

10 PHPUnit tests verify every branch, the forged-signature rejection,
the blank-secret skip, the ordering helper, and the InboundMessage
body() text/html preference logic.
Closes the core inbound webhook for Symfony end-to-end (stacked on
inbound set — .NET #24, Spring #27, Go #30, Phoenix #36, Symfony.

- PostmarkInboundParser: normalizes Postmark's JSON webhook payload
  (FromFull / ToFull / Headers / Attachments) into InboundMessage.
  Extracts threading headers from the Headers array. Decodes base64
  attachment content inline via base64_decode with strict=true;
  falls back gracefully to null when decoding fails.

- InboundEmailController (#[Route] -attributed): POST
  /escalated/webhook/email/inbound
  - Dispatches to the matching parser by ?adapter=... query or
    X-Escalated-Adapter header. Parsers are injected via
    #[TaggedIterator('escalated.inbound_parser')] so host apps can
    register additional providers just by tagging their service.
  - Signature-guarded by X-Escalated-Inbound-Secret (constant-time
    hash_equals compare). Same secret that signs Reply-To —
    symmetric.
  - Returns 202 Accepted with { status, ticket_id } on success,
    401 on secret mismatch, 400 on unknown adapter / invalid
    payload.

5 parser tests cover field extraction, threading header parsing,
base64 attachment decoding, minimal-payload path, and the adapter
name contract — all pass locally.
Adds Mailgun as the second supported inbound provider alongside
Postmark (from #31). Completes Mailgun parity across all 5
greenfield frameworks — .NET #25, Spring #28, Go #31, Phoenix #37,
Symfony.

Register the service with the escalated.inbound_parser tag and the
controller picks it up via TaggedIterator alongside PostmarkParser.

Notes:
- from: typically 'Name <email>' — extractFromName pulls the
  display portion, strips surrounding quotes. Falls back to sender
  field for the email.
- Mailgun hosts attachment content behind a URL; we carry the URL
  in downloadUrl. A follow-up worker fetches + persists out-of-band.
- Malformed attachments JSON degrades gracefully (empty list).

8 PHPUnit tests verified locally (8 tests, 19 assertions, OK).
Mirrors the .NET / Spring / Go / Phoenix orchestration port:
- InboundEmailService::process() composes InboundRouter::resolveTicket
  with TicketService, returning a ProcessResult carrying outcome,
  ticketId, replyId, and pendingAttachmentDownloads.
- TicketService::addInboundEmailReply() lets the service write a reply
  with no authenticated author (authorClass="inbound_email").
- Controller now calls the service and returns the richer response
  (status + outcome + ticket_id + reply_id + pending_attachment_downloads).

Tests cover: matched reply, new ticket creation, empty-subject
fallback, SNS skip, empty body+subject skip, provider-hosted
attachment surfacing, and the IsNoiseEmail matrix.
@mpge mpge force-pushed the feat/inbound-email-orchestration branch from bac8899 to 31895ac Compare April 27, 2026 02:12
@mpge mpge merged commit c504fa5 into master Apr 27, 2026
1 check passed
@mpge mpge deleted the feat/inbound-email-orchestration branch April 27, 2026 02:16
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