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()