Skip to content

fix(messaging): decode HTML entities in the plain-text part of emails - #14118

Merged
HarshMN2345 merged 13 commits into
mainfrom
fix/email-text-part-entities
Oct 22, 2026
Merged

HarshMN2345 merged 13 commits into
mainfrom
fix/email-text-part-entities

Conversation

@HarshMN2345

Copy link
Copy Markdown
Member

Mime::text() builds the plain-text part by stripping tags from the HTML body, but never decodes entities. Every HTML email therefore has literal entities in its text part. In auth emails that means "If you didn't ask to reset your password…", followed by the preview padding as a long run of  ‌… text.

This decodes entities after stripping tags (ENT_QUOTES | ENT_HTML5, UTF-8).

Verification: I rendered the recovery email through the real Mails worker into a local SMTP sink and scored it with rspamc (rspamd 4.2.1).

Related: #14089, #14111

The text part is the HTML body with tags stripped, so entities stayed
literal: apostrophes showed as ' and the preview padding as
‌ ... runs.
@ghost

ghost commented Oct 5, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

The remaining open finding requests an explicit assertion that plain-text input is passed through unchanged; the new carriage-return handling looks correct.

The messaging package now builds HTML emails’ plain-text alternatives from parsed visible content, decoding entities, omitting non-visible blocks, and retaining useful link destinations. It also declares the DOM extension and adds MIME tests for the decoded alternative and preserved HTML body. The latest change treats carriage returns as collapsible whitespace during rendering and preview-padding detection.

Latest changes: The newest commit adds carriage returns to the whitespace handling for ordinary text nodes and preview-padding nodes.

Verdict New comments Fixed Still open
✅ Approved 0 1 1
Fix with agent prompt
### Issue 1
packages/messaging/src/Adapter/Email/Mime.php:117-119
**Add a regression test for entity decoding**

The existing `MimeTest::testMarkupBringsAPlainAlternativeWithIt` contains no entities, so it passes even if this fix is removed. Please add coverage for decoded named/numeric entities (including quotes and escaped angle brackets) in the plain alternative, and verify that HTML and plain-text input remain unchanged.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
📂 Walkthrough · 3
File Change
packages/messaging/composer.json Declares the DOM extension required by the HTML parser.
packages/messaging/src/Adapter/Email/Mime.php Generates readable plain-text alternatives from HTML, including entity decoding, block separation, link destinations, and whitespace normalization.
packages/messaging/tests/Adapter/Email/MimeTest.php Covers entity decoding in the plain alternative and preservation of the encoded HTML part.
⏳ Still open from earlier reviews · 1
✅ Fixed since the last review · 1
  • Keep collapsing carriage returns in normal text · packages/messaging/src/Adapter/Email/Mime.php:151

Reviewed the commits since 33fc274 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

Comment on lines +117 to +119
return trim(html_entity_decode(strip_tags(
preg_replace('/<style\b[^>]*>(.*?)<\/style>/is', '', $email->getContent()) ?? '',
));
), ENT_QUOTES | ENT_HTML5, 'UTF-8'));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a regression test for entity decoding

The existing MimeTest::testMarkupBringsAPlainAlternativeWithIt contains no entities, so it passes even if this fix is removed. Please add coverage for decoded named/numeric entities (including quotes and escaped angle brackets) in the plain alternative, and verify that HTML and plain-text input remain unchanged.

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/messaging/src/Adapter/Email/Mime.php
Line: 117-119

Comment:
**Add a regression test for entity decoding**

The existing `MimeTest::testMarkupBringsAPlainAlternativeWithIt` contains no entities, so it passes even if this fix is removed. Please add coverage for decoded named/numeric entities (including quotes and escaped angle brackets) in the plain alternative, and verify that HTML and plain-text input remain unchanged.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

🟡 Minor · testing · Reply if this doesn't apply.

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/email-text-part-entities (after).

Metric Before After Change
🚀 Requests/sec 152.63 190.72 🟢 +25%
⏱️ Latency P50 109.58 ms 89.52 ms 🟢 -18.3%
⏱️ Latency P95 272.48 ms 213.94 ms 🟢 -21.5%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 89.52 213.94 11,970 190.72 -58.54
Account 172.08 314.87 630 10.45 -97.67
TablesDB 86.22 164 6,510 106.21 -53.71
Storage 83.67 192.96 3,150 53.45 -42.07
Functions 127.23 257.29 1,680 29.18 -48.36

Top API waits (after)

API request Max wait (ms)
account.name.update 494.3
tablesdb.rows.update 483.98
account.prefs.update 400.63
storage.files.create 392.7
functions.variables.update 379.74

Named and numeric entities, quotes and escaped angle brackets read as
text in the plain alternative, while the HTML part keeps them encoded.
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

Walk the parsed document instead of stripping tags: skip head, style,
script and hidden preheaders, collapse source whitespace, break lines at
blocks and <br>, and keep each link's address beside its label so a
button still leads somewhere.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

HarshMN2345 and others added 3 commits October 21, 2026 13:51
The preheader is the email's summary and readers of the text part
expect it, so only its invisible padding is dropped.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

HarshMN2345 and others added 5 commits October 22, 2026 15:18
… the plain-text part

Drop invisible characters only from a text node made of nothing else,
so joiners and direction marks in real text survive; keep whitespace
inside pre as written; declare ext-dom.
The padding check above already returns for a text node with no words.
The parser always creates a body, and a childless non-element node
already renders to nothing through the general path.
Rendering starts at the body, so head is never visited; the parser
lowercases HTML element names, never makes an empty text node and
normalizes carriage returns to newlines.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

The parser normalizes source line endings, but &#13; still puts a
carriage return in a text node, which the plain-text part would turn
into a line break.
@HarshMN2345
HarshMN2345 merged commit 83cbe41 into main Oct 22, 2026
31 checks passed
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…ntities

fix(messaging): decode HTML entities in the plain-text part of emails

(cherry picked from commit 83cbe41)
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