feat: 收件人/发件人改用邮件原始头数据
- sender 从 decode 改为使用原始 From 头 - 新增 recipient 字段,使用原始 To 头 - 移除 ai_process 中对 acct.username 的依赖
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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" 已发送并标记已读")
|
||||
|
||||
@@ -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", ""),
|
||||
|
||||
Reference in New Issue
Block a user