feat: handle Reply-To header, add account_email field, decode address headers
This commit is contained in:
@@ -38,10 +38,10 @@ SYSTEM_PROMPT = """你是一个邮件摘要助手。请分析邮件内容并以
|
|||||||
|
|
||||||
|
|
||||||
@retry()
|
@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", " ")
|
body_preview = body[:80].replace("\n", " ")
|
||||||
logger.info("AI 摘要请求: sender=%s subject=%s body_preview=%s", sender, subject, body_preview)
|
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 = {
|
payload = {
|
||||||
"model": ai_cfg.model,
|
"model": ai_cfg.model,
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import imaplib
|
|||||||
import email
|
import email
|
||||||
import logging
|
import logging
|
||||||
from email.header import decode_header
|
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 typing import Optional
|
||||||
from src.config import EmailAccount
|
from src.config import EmailAccount
|
||||||
from src.retry import retry
|
from src.retry import retry
|
||||||
@@ -11,13 +11,16 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class Email:
|
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.uid = uid
|
||||||
self.subject = subject
|
self.subject = subject
|
||||||
self.sender = sender
|
self.sender = sender
|
||||||
self.recipient = recipient
|
self.recipient = recipient
|
||||||
self.body = body
|
self.body = body
|
||||||
self.date = date
|
self.date = date
|
||||||
|
self.reply_to = reply_to
|
||||||
|
self.account_email = account_email
|
||||||
|
|
||||||
|
|
||||||
def _decode_str(s: str) -> str:
|
def _decode_str(s: str) -> str:
|
||||||
@@ -34,6 +37,18 @@ def _decode_str(s: str) -> str:
|
|||||||
return "".join(result)
|
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:
|
def _get_text_from_msg(msg) -> str:
|
||||||
if msg.is_multipart():
|
if msg.is_multipart():
|
||||||
for part in msg.walk():
|
for part in msg.walk():
|
||||||
@@ -108,14 +123,16 @@ def fetch_unseen_emails(account: EmailAccount) -> list[Email]:
|
|||||||
msg = email.message_from_bytes(raw_email)
|
msg = email.message_from_bytes(raw_email)
|
||||||
|
|
||||||
subject = _decode_str(msg.get("Subject", ""))
|
subject = _decode_str(msg.get("Subject", ""))
|
||||||
sender = msg.get("From", "")
|
sender = _decode_address_header(msg.get("From", ""))
|
||||||
recipient = msg.get("To", "")
|
recipient = _decode_address_header(msg.get("To", ""))
|
||||||
|
reply_to = _decode_address_header(msg.get("Reply-To", ""))
|
||||||
date_str = msg.get("Date", "")
|
date_str = msg.get("Date", "")
|
||||||
body = _get_text_from_msg(msg)
|
body = _get_text_from_msg(msg)
|
||||||
body_len = len(body)
|
body_len = len(body)
|
||||||
|
|
||||||
logger.info(" 邮件: [%s] from=%s to=%s (%d 字符)", subject, sender, recipient, body_len)
|
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))
|
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()
|
conn.logout()
|
||||||
logger.info("共获取 %d 封新邮件", len(emails))
|
logger.info("共获取 %d 封新邮件", len(emails))
|
||||||
|
|||||||
@@ -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]:
|
def ai_process(cfg: Config, acct_idx: int, mail: Email) -> Optional[dict]:
|
||||||
logger.info(f" 正在摘要: {mail.subject}")
|
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
|
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 {
|
return {
|
||||||
"text": text,
|
"text": text,
|
||||||
"data": summary,
|
"data": summary,
|
||||||
"original_body": mail.body,
|
"original_body": mail.body,
|
||||||
"original_sender": mail.sender,
|
"original_sender": mail.sender,
|
||||||
"original_recipient": mail.recipient,
|
"original_recipient": mail.recipient,
|
||||||
|
"original_reply_to": mail.reply_to,
|
||||||
"acct_idx": acct_idx,
|
"acct_idx": acct_idx,
|
||||||
|
"account_email": mail.account_email,
|
||||||
"uid": mail.uid,
|
"uid": mail.uid,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,6 +47,8 @@ def tg_send_and_mark(cfg: Config, info: dict):
|
|||||||
info["text"], info["data"],
|
info["text"], info["data"],
|
||||||
info["original_body"], info["acct_idx"],
|
info["original_body"], info["acct_idx"],
|
||||||
original_sender=info.get("original_sender", ""),
|
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"]])
|
mark_as_seen(acct, [info["uid"]])
|
||||||
logger.info(f" 已发送并标记已读")
|
logger.info(f" 已发送并标记已读")
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ _conversations: dict[int, dict] = {}
|
|||||||
@retry()
|
@retry()
|
||||||
def send_summary(tg_cfg: TelegramConfig, chat_id: str, summary_text: str,
|
def send_summary(tg_cfg: TelegramConfig, chat_id: str, summary_text: str,
|
||||||
summary_data: dict, original_body: str, account_idx: int,
|
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)
|
can_reply = summary_data.get("can_reply", True)
|
||||||
result = _tg_req(tg_cfg, "sendMessage", {
|
result = _tg_req(tg_cfg, "sendMessage", {
|
||||||
"chat_id": chat_id,
|
"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_body": original_body[:10000],
|
||||||
"original_sender": original_sender or summary_data.get("sender", ""),
|
"original_sender": original_sender or summary_data.get("sender", ""),
|
||||||
"original_recipient": original_recipient or summary_data.get("recipient", ""),
|
"original_recipient": original_recipient or summary_data.get("recipient", ""),
|
||||||
|
"original_reply_to": original_reply_to,
|
||||||
"account_idx": account_idx,
|
"account_idx": account_idx,
|
||||||
|
"account_email": account_email,
|
||||||
"sender": summary_data.get("sender", ""),
|
"sender": summary_data.get("sender", ""),
|
||||||
"subject": summary_data.get("subject", ""),
|
"subject": summary_data.get("subject", ""),
|
||||||
"can_reply": can_reply,
|
"can_reply": can_reply,
|
||||||
@@ -89,7 +92,7 @@ def poll_telegram(tg_cfg: TelegramConfig, cfg: Config, last_update_id: int) -> i
|
|||||||
|
|
||||||
# ── Formatting ──────────────────────────────────────────
|
# ── Formatting ──────────────────────────────────────────
|
||||||
|
|
||||||
def format_summary(data: dict) -> str:
|
def format_summary(data: dict, account_email: str = "") -> str:
|
||||||
priority = data.get("priority", "medium")
|
priority = data.get("priority", "medium")
|
||||||
icon = _priority_icon.get(priority, "⚪")
|
icon = _priority_icon.get(priority, "⚪")
|
||||||
lines = [
|
lines = [
|
||||||
@@ -97,6 +100,7 @@ def format_summary(data: dict) -> str:
|
|||||||
"━━━━━━━━━━━━━━━━━━",
|
"━━━━━━━━━━━━━━━━━━",
|
||||||
f"*收件人:* {_escape(data.get('recipient', '未知'))}",
|
f"*收件人:* {_escape(data.get('recipient', '未知'))}",
|
||||||
f"*发件人:* {_escape(data.get('sender', '未知'))}",
|
f"*发件人:* {_escape(data.get('sender', '未知'))}",
|
||||||
|
f"*账户:* {_escape(account_email)}",
|
||||||
f"*优先级:* {icon} {priority.upper()}",
|
f"*优先级:* {icon} {priority.upper()}",
|
||||||
"",
|
"",
|
||||||
_escape(data.get("summary", "")),
|
_escape(data.get("summary", "")),
|
||||||
@@ -407,7 +411,9 @@ def _do_send_reply(tg_cfg: TelegramConfig, cfg: Config,
|
|||||||
if not acct.smtp:
|
if not acct.smtp:
|
||||||
send_text(tg_cfg, str(chat_id), "❌ 该邮箱未配置 SMTP,无法发送回复。")
|
send_text(tg_cfg, str(chat_id), "❌ 该邮箱未配置 SMTP,无法发送回复。")
|
||||||
return
|
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:
|
if not to_addr:
|
||||||
to_addr = parseaddr(ctx["sender"])[1]
|
to_addr = parseaddr(ctx["sender"])[1]
|
||||||
if not to_addr:
|
if not to_addr:
|
||||||
|
|||||||
Reference in New Issue
Block a user