From ad950ff3672a4645a42e36a41a0d047f0c9e1795 Mon Sep 17 00:00:00 2001 From: Zichao Lin Date: Thu, 2 Jul 2026 22:04:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=B6=E4=BB=B6=E4=BA=BA/=E5=8F=91?= =?UTF-8?q?=E4=BB=B6=E4=BA=BA=E6=94=B9=E7=94=A8=E9=82=AE=E4=BB=B6=E5=8E=9F?= =?UTF-8?q?=E5=A7=8B=E5=A4=B4=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - sender 从 decode 改为使用原始 From 头 - 新增 recipient 字段,使用原始 To 头 - 移除 ai_process 中对 acct.username 的依赖 --- src/email_client.py | 10 ++++++---- src/summarizer.py | 9 +++++---- src/tg_bot.py | 3 ++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/email_client.py b/src/email_client.py index 6417eb0..f193abc 100644 --- a/src/email_client.py +++ b/src/email_client.py @@ -11,10 +11,11 @@ logger = logging.getLogger(__name__) class Email: - def __init__(self, uid: bytes, subject: str, sender: str, body: str, date: str): + def __init__(self, uid: bytes, subject: str, sender: str, recipient: str, body: str, date: str): self.uid = uid self.subject = subject self.sender = sender + self.recipient = recipient self.body = body self.date = date @@ -102,13 +103,14 @@ def fetch_unseen_emails(account: EmailAccount) -> list[Email]: msg = email.message_from_bytes(raw_email) subject = _decode_str(msg.get("Subject", "")) - sender = _decode_str(msg.get("From", "")) + sender = msg.get("From", "") + recipient = msg.get("To", "") date_str = msg.get("Date", "") body = _get_text_from_msg(msg) body_len = len(body) - logger.info(" 邮件: [%s] %s <%s> (%d 字符)", subject, sender, date_str, body_len) - emails.append(Email(uid=uid, subject=subject, sender=sender, body=body, date=date_str)) + logger.info(" 邮件: [%s] from=%s to=%s (%d 字符)", subject, sender, recipient, body_len) + emails.append(Email(uid=uid, subject=subject, sender=sender, recipient=recipient, body=body, date=date_str)) conn.logout() logger.info("共获取 %d 封新邮件", len(emails)) diff --git a/src/summarizer.py b/src/summarizer.py index 4946d05..2e595fd 100644 --- a/src/summarizer.py +++ b/src/summarizer.py @@ -22,16 +22,16 @@ def poll_accounts(cfg: Config) -> Generator[tuple[int, Email], None, None]: def ai_process(cfg: Config, acct_idx: int, mail: Email) -> Optional[dict]: - acct = cfg.email_accounts[acct_idx] logger.info(f" 正在摘要: {mail.subject}") - summary = summarize_email(cfg.ai, acct.username, mail.subject, mail.sender, mail.body) - summary["recipient"] = acct.username + summary = summarize_email(cfg.ai, mail.recipient, mail.subject, mail.sender, mail.body) + summary["recipient"] = mail.recipient text = format_summary(summary) return { "text": text, "data": summary, "original_body": mail.body, "original_sender": mail.sender, + "original_recipient": mail.recipient, "acct_idx": acct_idx, "uid": mail.uid, } @@ -42,6 +42,7 @@ def tg_send_and_mark(cfg: Config, info: dict): send_summary(cfg.telegram, cfg.telegram.chat_id, info["text"], info["data"], info["original_body"], info["acct_idx"], - original_sender=info.get("original_sender", "")) + original_sender=info.get("original_sender", ""), + original_recipient=info.get("original_recipient", "")) mark_as_seen(acct, [info["uid"]]) logger.info(f" 已发送并标记已读") diff --git a/src/tg_bot.py b/src/tg_bot.py index ad93256..90a471b 100644 --- a/src/tg_bot.py +++ b/src/tg_bot.py @@ -22,7 +22,7 @@ _conversations: dict[int, dict] = {} @retry() def send_summary(tg_cfg: TelegramConfig, chat_id: str, summary_text: str, summary_data: dict, original_body: str, account_idx: int, - original_sender: str = "") -> int: + original_sender: str = "", original_recipient: str = "") -> int: can_reply = summary_data.get("can_reply", True) result = _tg_req(tg_cfg, "sendMessage", { "chat_id": chat_id, @@ -36,6 +36,7 @@ def send_summary(tg_cfg: TelegramConfig, chat_id: str, summary_text: str, "summary_data": summary_data, "original_body": original_body[:10000], "original_sender": original_sender or summary_data.get("sender", ""), + "original_recipient": original_recipient or summary_data.get("recipient", ""), "account_idx": account_idx, "sender": summary_data.get("sender", ""), "subject": summary_data.get("subject", ""),