Repository navigation
fix(messaging): refuse undeliverable email input with a typed exception - #321
Merged
Merged
Conversation
Resend answered "Invalid `to` field" for 269 cloud mails in two weeks. Most were recipients at example.com or at domains with no real TLD (c.c, a.i), which no provider can route to; the rest were real addresses whose display name carried a comma or angle bracket that the unquoted `Name <email>` rendering turned into a malformed header. Every one reached the provider, cost a request, and came back as free-form text that callers had to pattern-match to decide whether to retry. Address syntax and display names are now checked when the message is built, reserved domains are refused by the hosted adapters before a request is spent (SMTP and Mock keep accepting them for local catchers), and display names are quoted per RFC 5322 by a formatter shared by Resend, Mailgun and SES. All of these throw `Exception\InvalidArgumentException`, a subclass of the SPL one so existing catches keep working, carrying a type constant and the offending value so callers can map the failure without parsing the message. Mailgun's Reply-To header was sent as a value-less form entry rather than `h:Reply-To => ...`; fixed while routing it through the formatter.
ChiragAgg5k
requested review from
Meldiron,
abnegate,
eldadfux and
loks0n
as code owners
September 22, 2026 10:48
Contributor
|
…checking domains Which domains a provider refuses is the provider's policy, not something to mirror in a hard-coded list that drifts. Resend already answers 422 for a recipient it cannot route to, so the adapter maps that refusal to the typed exception with the provider's own message. On the per-recipient path a 422 stays a per-recipient failure when there are several recipients, so the rest still deliver.
Member
Author
|
@greptile review |
Member
Author
|
@greptile review |
Member
Author
|
@greptile review |
loks0n
approved these changes
Sep 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Resend answered
Invalidtofieldfor 269 cloud mails over two weeks (CLOUD-3QX0). Sampling the events gave two causes:example.comor at domains with no real TLD (c.c,a.i,gyung.me976153), which no provider can route to. Resend blocks reserved domains with a 422 by policy.gmail.comaddresses whose display name carried a comma or angle bracket. The Resend and Mailgun adapters interpolatedName <email>unquoted, so the name broke the header grammar.Every one of these reached the provider, cost a request, and came back as free-form text that Appwrite had to pattern-match to decide whether to retry.
What
Messages\Emailvalidates every to, cc, bcc, from and reply-to address (syntax plus a real TLD) and rejects display names with control characters at construction. Specials such as<,,and"are allowed and quoted later.Adapter\Email::formatAddress()(moved from SES) renders addresses per RFC 5322 for Resend, Mailgun and SES."Doe, John <JD>" <[email protected]>instead of a malformed header.Exception\InvalidArgumentExceptionextends the SPL one, so existing catches keep working.getType()returns a constant (recipient_malformed,recipient_domain_invalid,provider_rejected,name_malformed,sender_malformed,too_many_recipients, ...) andgetValue()the offending input.Adapter::send()andMessages\Pushuse it for their existing checks.h:Reply-To => ....Tests
tests/Messaging/Messages/EmailTest.phpcovers each type, andResendRoutingTestasserts the quotedto,from,reply_toandccpayload and that a 422 from Resend surfaces as the typed exception. Each new test was run against the previous production code and fails there.Usage
Building a message with input no provider can deliver throws before anything is sent:
A request Resend refuses as malformed surfaces the same way, carrying Resend's message:
Display names with specials are quoted for the provider instead of breaking the header:
A worker maps the type to its own handling instead of matching the provider's message text:
Follow-up in Appwrite
The cloud
Mailsworker and the CEMessagingworker should catchUtopia\Messaging\Exception\InvalidArgumentException, skip without retry, and record the type on the span. That replaces the string matching inisRetryableError()for this class of failure.