fix: mark emails back as UNSEEN after FETCH to counter server auto-read

This commit is contained in:
2026-07-11 22:45:31 +08:00
parent 5320b10317
commit 1c7d0c1b00

View File

@@ -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: