Issue Description
A lot of my marketing mail gets a broken preview in Stalwart. Some show raw CSS, others show nothing at all. I traced it to two bugs in the HTML-to-text conversion in mail-parser (html_to_text). Stalwart uses it for the preview of HTML-only messages and to index HTML parts for search.
- Style and script blocks are only skipped when the tag has no attributes. A style tag with type=“text/css” inside the body isn’t recognised, so the whole style sheet ends up in the text.
- An HTML comment that ends with more than two dashes never closes. The empty comment that many newsletters use is one example. Everything after it is lost.
- A Wordfence activity report (HTML only) shows its CSS as the preview: “blockquote:before { content: none !important; } blockquote:after { …”
- A Lieferando newsletter gets an empty preview, because an empty comment in its head swallows the rest of the HTML part.
Expected Behavior
Style and script blocks are skipped whatever attributes the tag has. A comment ends correctly no matter how many dashes come before the closing bracket, which is how browsers handle it.
For the test messages below, the preview should be “Hello world” or “Before Hello world”.
Actual Behavior
HTML body, then the preview Stalwart returns:
<style>p { color: red; }</style><p>Hello world</p>
-> Hello world (correct)
<style type="text/css">p { color: red; }</style><p>Hello world</p>
-> p { color: red; }Hello world
<script type="text/javascript">var x = 1;</script><p>Hello world</p>
-> var x = 1;Hello world
<p>Before</p><!----><p>Hello world</p>
-> Before
<p>Before</p><!-- note ---><p>Hello world</p>
-> Before
Reproduction Steps
- Deliver or import this message:
From: [email protected]
To: [email protected]
Subject: style test
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
<html><head><title>t</title></head><body><style type="text/css">p { color: red; }</style><p>Hello world</p></body></html>
- Fetch it with Email/get and ask for the preview property. You get “p { color: red; }Hello world”. Remove type=“text/css” and you get “Hello world”.
- Do the same with the body from the last two examples above. The preview is just “Before”.
Relevant Log Output
- In mail-parser 0.11.9 (src/decoders/html.rs), the tag name is only compared while tag_token_pos is 1. A tag with any attribute never matches, so in_style and in_script stay false. A comment only ends when exactly two dashes come before the closing bracket (comment_pos == 2).
- It still happens on mail-parser main, the unreleased 1.0.0 rewrite. I built it and ran the same input.
- Search indexing (crates/email/src/message/index/search.rs) and search snippets use the same function. So in HTML-only messages, text after such a comment probably isn’t searchable either. I haven’t tested that part.
Stalwart Version
v0.16.x
Installation Method
Built from source
Database Backend
RocksDB
Blob Storage
RocksDB
Search Engine
Internal
Directory Backend
Internal
I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.
on
I have searched this support forum (open and closed topics) and confirm this is not a duplicate.
on
I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.
on