From ada760cc43a227e8e6e181a93e051c26f8d58506 Mon Sep 17 00:00:00 2001 From: Yigit Colakoglu Date: Tue, 30 Dec 2025 16:18:51 -0800 Subject: [PATCH] Fix bytes/string handling in email service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IMAP data can be bytes; handle both in decode_mime_header and _get_text_snippet. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- src/services/email_service.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/services/email_service.py b/src/services/email_service.py index 59fea2a..bf652e3 100644 --- a/src/services/email_service.py +++ b/src/services/email_service.py @@ -22,9 +22,12 @@ from models.common import OperationResult from config import Settings -def decode_mime_header(header: Optional[str]) -> str: +def decode_mime_header(header) -> str: if not header: return "" + # Handle bytes input from IMAP + if isinstance(header, bytes): + header = header.decode("utf-8", errors="replace") decoded_parts = [] for part, encoding in decode_header(header): if isinstance(part, bytes): @@ -516,7 +519,14 @@ class EmailService: body_text, body_html = self._get_body(msg) text = body_text or "" + # Ensure text is a string + if isinstance(text, bytes): + text = text.decode("utf-8", errors="replace") + if not text and body_html: + # Ensure body_html is a string before regex + if isinstance(body_html, bytes): + body_html = body_html.decode("utf-8", errors="replace") # Strip HTML tags for snippet text = re.sub(r"<[^>]+>", "", body_html) text = re.sub(r"\s+", " ", text).strip()