feat: handle Reply-To header, add account_email field, decode address headers

This commit is contained in:
2026-07-02 22:37:15 +08:00
parent d7daddf491
commit c684090b9c
4 changed files with 43 additions and 14 deletions

View File

@@ -38,10 +38,10 @@ SYSTEM_PROMPT = """你是一个邮件摘要助手。请分析邮件内容并以
@retry()
def summarize_email(ai_cfg: AIConfig, recipient: str, subject: str, sender: str, body: str) -> dict[str, Any]:
def summarize_email(ai_cfg: AIConfig, recipient: str, subject: str, sender: str, body: str, account: str = "") -> dict[str, Any]:
body_preview = body[:80].replace("\n", " ")
logger.info("AI 摘要请求: sender=%s subject=%s body_preview=%s", sender, subject, body_preview)
content = f"收件人: {recipient}\n发件人: {sender}\n主题: {subject}\n正文:\n{body[:4000]}"
content = f"收件人: {recipient}\n发件人: {sender}\n主题: {subject}\n账号: {account}\n正文:\n{body[:4000]}"
payload = {
"model": ai_cfg.model,

View File

@@ -2,7 +2,7 @@ import imaplib
import email
import logging
from email.header import decode_header
from email.utils import parsedate_to_datetime
from email.utils import parsedate_to_datetime, parseaddr
from typing import Optional
from src.config import EmailAccount
from src.retry import retry
@@ -11,13 +11,16 @@ logger = logging.getLogger(__name__)
class Email:
def __init__(self, uid: bytes, subject: str, sender: str, recipient: str, body: str, date: str):
def __init__(self, uid: bytes, subject: str, sender: str, recipient: str,
body: str, date: str, reply_to: str = "", account_email: str = ""):
self.uid = uid
self.subject = subject
self.sender = sender
self.recipient = recipient
self.body = body
self.date = date
self.reply_to = reply_to
self.account_email = account_email
def _decode_str(s: str) -> str:
@@ -34,6 +37,18 @@ def _decode_str(s: str) -> str:
return "".join(result)
def _decode_address_header(header_value: str) -> str:
if not header_value:
return ""
decoded = _decode_str(header_value)
name, addr = parseaddr(decoded)
if not addr:
return name
if name:
return f"{name} <{addr}>"
return addr
def _get_text_from_msg(msg) -> str:
if msg.is_multipart():
for part in msg.walk():
@@ -108,14 +123,16 @@ def fetch_unseen_emails(account: EmailAccount) -> list[Email]:
msg = email.message_from_bytes(raw_email)
subject = _decode_str(msg.get("Subject", ""))
sender = msg.get("From", "")
recipient = msg.get("To", "")
sender = _decode_address_header(msg.get("From", ""))
recipient = _decode_address_header(msg.get("To", ""))
reply_to = _decode_address_header(msg.get("Reply-To", ""))
date_str = msg.get("Date", "")
body = _get_text_from_msg(msg)
body_len = len(body)
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))
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))
conn.logout()
logger.info("共获取 %d 封新邮件", len(emails))

View File

@@ -23,16 +23,20 @@ def poll_accounts(cfg: Config) -> Generator[tuple[int, Email], None, None]:
def ai_process(cfg: Config, acct_idx: int, mail: Email) -> Optional[dict]:
logger.info(f" 正在摘要: {mail.subject}")
summary = summarize_email(cfg.ai, mail.recipient, mail.subject, mail.sender, mail.body)
summary = summarize_email(cfg.ai, mail.recipient, mail.subject, mail.sender, mail.body, mail.account_email)
summary["recipient"] = mail.recipient
text = format_summary(summary)
if "@" not in summary.get("sender", ""):
summary["sender"] = mail.sender
text = format_summary(summary, mail.account_email)
return {
"text": text,
"data": summary,
"original_body": mail.body,
"original_sender": mail.sender,
"original_recipient": mail.recipient,
"original_reply_to": mail.reply_to,
"acct_idx": acct_idx,
"account_email": mail.account_email,
"uid": mail.uid,
}
@@ -43,6 +47,8 @@ def tg_send_and_mark(cfg: Config, info: dict):
info["text"], info["data"],
info["original_body"], info["acct_idx"],
original_sender=info.get("original_sender", ""),
original_recipient=info.get("original_recipient", ""))
original_recipient=info.get("original_recipient", ""),
original_reply_to=info.get("original_reply_to", ""),
account_email=info.get("account_email", ""))
mark_as_seen(acct, [info["uid"]])
logger.info(f" 已发送并标记已读")

View File

@@ -22,7 +22,8 @@ _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 = "", original_recipient: str = "") -> int:
original_sender: str = "", original_recipient: str = "",
original_reply_to: str = "", account_email: str = "") -> int:
can_reply = summary_data.get("can_reply", True)
result = _tg_req(tg_cfg, "sendMessage", {
"chat_id": chat_id,
@@ -37,7 +38,9 @@ def send_summary(tg_cfg: TelegramConfig, chat_id: str, summary_text: str,
"original_body": original_body[:10000],
"original_sender": original_sender or summary_data.get("sender", ""),
"original_recipient": original_recipient or summary_data.get("recipient", ""),
"original_reply_to": original_reply_to,
"account_idx": account_idx,
"account_email": account_email,
"sender": summary_data.get("sender", ""),
"subject": summary_data.get("subject", ""),
"can_reply": can_reply,
@@ -89,7 +92,7 @@ def poll_telegram(tg_cfg: TelegramConfig, cfg: Config, last_update_id: int) -> i
# ── Formatting ──────────────────────────────────────────
def format_summary(data: dict) -> str:
def format_summary(data: dict, account_email: str = "") -> str:
priority = data.get("priority", "medium")
icon = _priority_icon.get(priority, "")
lines = [
@@ -97,6 +100,7 @@ def format_summary(data: dict) -> str:
"━━━━━━━━━━━━━━━━━━",
f"*收件人:* {_escape(data.get('recipient', '未知'))}",
f"*发件人:* {_escape(data.get('sender', '未知'))}",
f"*账户:* {_escape(account_email)}",
f"*优先级:* {icon} {priority.upper()}",
"",
_escape(data.get("summary", "")),
@@ -407,7 +411,9 @@ def _do_send_reply(tg_cfg: TelegramConfig, cfg: Config,
if not acct.smtp:
send_text(tg_cfg, str(chat_id), "❌ 该邮箱未配置 SMTP无法发送回复。")
return
to_addr = parseaddr(ctx["original_sender"])[1]
to_addr = parseaddr(ctx.get("original_reply_to", ""))[1]
if not to_addr:
to_addr = parseaddr(ctx["original_sender"])[1]
if not to_addr:
to_addr = parseaddr(ctx["sender"])[1]
if not to_addr: