fix: update email_client and smtp_client for new config structure - email_client: use account.imap.server/port/ssl/starttls instead of flat fields - smtp_client: use s.ssl/starttls instead of s.use_ssl/use_starttls - tg_bot: fix _do_send_reply to use find_user_by_chat_id for email_accounts
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,5 +1,5 @@
|
||||
.venv/
|
||||
__pycache__/
|
||||
*.pyc
|
||||
config.yaml
|
||||
config.json
|
||||
data/
|
||||
|
||||
@@ -91,12 +91,13 @@ def _select_mailbox(conn, mailbox: str = "INBOX"):
|
||||
|
||||
|
||||
def _login_and_prepare(account: EmailAccount, timeout: int = 30, select_inbox: bool = True):
|
||||
logger.info("连接 %s:%d (超时%ds)", account.imap_server, account.imap_port, timeout)
|
||||
if account.use_ssl:
|
||||
conn = imaplib.IMAP4_SSL(account.imap_server, account.imap_port, timeout=timeout)
|
||||
imap = account.imap
|
||||
logger.info("连接 %s:%d (超时%ds)", imap.server, imap.port, timeout)
|
||||
if imap.ssl:
|
||||
conn = imaplib.IMAP4_SSL(imap.server, imap.port, timeout=timeout)
|
||||
else:
|
||||
conn = imaplib.IMAP4(account.imap_server, account.imap_port, timeout=timeout)
|
||||
if account.use_starttls:
|
||||
conn = imaplib.IMAP4(imap.server, imap.port, timeout=timeout)
|
||||
if imap.starttls:
|
||||
conn.starttls()
|
||||
conn.login(account.username, account.password)
|
||||
logger.info("登录成功: %s", account.username)
|
||||
|
||||
@@ -23,12 +23,12 @@ def send_reply(account: EmailAccount, to_addr: str, subject: str, body: str,
|
||||
raise RuntimeError(f"账号 {account.username} 未配置 SMTP")
|
||||
|
||||
s = account.smtp
|
||||
logger.info("SMTP 连接: %s:%d (ssl=%s)", s.server, s.port, s.use_ssl)
|
||||
if s.use_ssl:
|
||||
logger.info("SMTP 连接: %s:%d (ssl=%s)", s.server, s.port, s.ssl)
|
||||
if s.ssl:
|
||||
conn = smtplib.SMTP_SSL(s.server, s.port, timeout=15)
|
||||
else:
|
||||
conn = smtplib.SMTP(s.server, s.port, timeout=15)
|
||||
if s.use_starttls:
|
||||
if s.starttls:
|
||||
conn.starttls()
|
||||
|
||||
conn.login(account.username, account.password)
|
||||
|
||||
@@ -659,7 +659,11 @@ def _extract_media(msg: dict) -> list[dict]:
|
||||
def _do_send_reply(bot_token: str, cfg: Config,
|
||||
chat_id: int, msg_id: int, ctx: dict, reply_text: str,
|
||||
attachments: list[dict] | None = None):
|
||||
acct = cfg.email_accounts[ctx["account_idx"]]
|
||||
user = find_user_by_chat_id(cfg, chat_id)
|
||||
if not user:
|
||||
send_text(bot_token, str(chat_id), "❌ 用户未找到。")
|
||||
return
|
||||
acct = user.email_accounts[ctx["account_idx"]]
|
||||
if not acct.smtp:
|
||||
send_text(bot_token, str(chat_id), "❌ 该邮箱未配置 SMTP,无法发送回复。")
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user