From d7daddf4913d5f9341ef21ffcb9577ab68af14eb Mon Sep 17 00:00:00 2001 From: Zichao Lin Date: Thu, 2 Jul 2026 22:18:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20IMAP=20=E6=94=AF=E6=8C=81=20SSL/STARTTL?= =?UTF-8?q?S=20=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml.example | 2 ++ src/config.py | 2 ++ src/email_client.py | 7 ++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/config.yaml.example b/config.yaml.example index 3e6de2c..454dba0 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -3,6 +3,7 @@ email_accounts: imap_port: 993 username: "your_email@gmail.com" password: "your_app_password" + use_ssl: true smtp: server: "smtp.gmail.com" port: 465 @@ -12,6 +13,7 @@ email_accounts: imap_port: 993 username: "your_email@126.com" password: "your_auth_code" + use_ssl: true smtp: server: "smtp.126.com" port: 465 diff --git a/src/config.py b/src/config.py index 3ab4f45..4882dbb 100644 --- a/src/config.py +++ b/src/config.py @@ -17,6 +17,8 @@ class EmailAccount: imap_port: int username: str password: str + use_ssl: bool = True + use_starttls: bool = False smtp: Optional[SmtpConfig] = None diff --git a/src/email_client.py b/src/email_client.py index f193abc..12c6608 100644 --- a/src/email_client.py +++ b/src/email_client.py @@ -74,7 +74,12 @@ def _select_mailbox(conn, mailbox: str = "INBOX"): def _login_and_prepare(account: EmailAccount): logger.info("连接 %s:%d", account.imap_server, account.imap_port) - conn = imaplib.IMAP4_SSL(account.imap_server, account.imap_port) + if account.use_ssl: + conn = imaplib.IMAP4_SSL(account.imap_server, account.imap_port) + else: + conn = imaplib.IMAP4(account.imap_server, account.imap_port) + if account.use_starttls: + conn.starttls() conn.login(account.username, account.password) logger.info("登录成功: %s", account.username) if _check_provider(account.username, _PROVIDERS_NEED_ID):