From 1c7d0c1b00a6232328f6903b6c20fa962ed8cad2 Mon Sep 17 00:00:00 2001 From: Zichao Lin Date: Sat, 11 Jul 2026 22:45:31 +0800 Subject: [PATCH] fix: mark emails back as UNSEEN after FETCH to counter server auto-read --- src/email_client.py | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/src/email_client.py b/src/email_client.py index 539a6c2..00ff9c2 100644 --- a/src/email_client.py +++ b/src/email_client.py @@ -115,18 +115,7 @@ def fetch_unseen_emails(account: EmailAccount) -> list[Email]: for uid in uids: try: - _, size_data = conn.uid("FETCH", uid, "(RFC822.SIZE)") - size = 0 - if size_data[0]: - m = re.search(rb"RFC822\.SIZE (\d+)", size_data[0]) - if m: - size = int(m.group(1)) - large = size > 200_000 - if large: - logger.info("UID %s 大邮件 (%d bytes),用 PEEK 拉取", uid, size) - - fetch_cmd = "BODY.PEEK[]" if large else "RFC822" - _, msg_data = conn.uid("FETCH", uid, fetch_cmd) + _, msg_data = conn.uid("FETCH", uid, "RFC822") if msg_data[0] is None: logger.warning("UID %s FETCH 返回空,跳过", uid) continue @@ -144,17 +133,11 @@ def fetch_unseen_emails(account: EmailAccount) -> list[Email]: logger.info(" 邮件: [%s] from=%s to=%s reply-to=%s (%d 字符)", subject, sender, recipient, reply_to, body_len) emails.append(Email(uid=uid, subject=subject, sender=sender, recipient=recipient, body=body, date=date_str, reply_to=reply_to, account_email=account.username)) + + # FETCH 可能被服务器自动标已读,立即标回未读 + conn.uid("STORE", uid, "-FLAGS", "\\Seen") except Exception as e: - logger.error("UID %s FETCH 失败,跳过并重建连接: %s", uid, e) - try: - conn.logout() - except Exception: - pass - try: - conn = _login_and_prepare(account) - except Exception as e2: - logger.error("重建连接失败: %s", e2) - break + logger.error("UID %s FETCH 失败,跳过: %s", uid, e) continue try: