Files
ewoooc/tests/test_imap_to_smtp.py
ogt 1b4f3a7bbe
Some checks failed
CD Pipeline / deploy (push) Failing after 59s
feat: EwoooC 初始化 — 完整專案推版至 Gitea
- 建立 Gitea Actions CD pipeline (.gitea/workflows/cd.yaml)
- 部署模式: rsync Python 檔案至 188 → docker restart (volume mount)
- Dockerfile/requirements 變動時自動重建 Docker image
- 部署通知: Telegram (開始/成功/失敗)
- 健康檢查: https://mo.wooo.work/health (最多 5 次重試)
- 同步最新 CLAUDE.md / ADR-008 / memory (2026-04-19)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 01:21:13 +08:00

128 lines
3.8 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
透過測試 IMAP 連線來推測 SMTP 設定
"""
import imaplib
import smtplib
import socket
def test_imap_server(host, port=993):
"""測試 IMAP 伺服器連線"""
try:
print(f"📥 測試 IMAP: {host}:{port}")
imap = imaplib.IMAP4_SSL(host, port, timeout=5)
print(f" ✅ IMAP 伺服器存在且可連接!")
imap.logout()
return True
except socket.gaierror:
print(f" ❌ DNS 解析失敗")
return False
except Exception as e:
print(f" ⚠️ {type(e).__name__}: {e}")
return False
def guess_smtp_from_imap(imap_host):
"""從 IMAP 主機推測 SMTP 主機"""
# 常見的對應關係
mappings = [
imap_host.replace('imap', 'smtp'),
imap_host.replace('mail', 'smtp'),
imap_host.replace('ms', 'smtp'),
imap_host, # 有些伺服器 IMAP 和 SMTP 用同一個
]
return list(set(mappings)) # 去重
def test_smtp_server(host, port):
"""測試 SMTP 伺服器"""
try:
print(f" 測試 SMTP: {host}:{port} ... ", end='')
server = smtplib.SMTP(host, port, timeout=5)
server.ehlo()
if server.has_extn('STARTTLS'):
server.starttls()
print(f"✅ 成功(支援 STARTTLS")
server.quit()
return True
else:
print(f"✅ 成功(不支援加密)")
server.quit()
return True
except Exception as e:
print(f"{type(e).__name__}")
return False
def main():
print("=" * 80)
print("🔍 PChome 郵件伺服器探測")
print("=" * 80)
# 嘗試常見的 PChome IMAP 伺服器
imap_servers = [
'imap.pchome.tw',
'imap.pchome.com.tw',
'mail.pchome.tw',
'mail.pchome.com.tw',
'ms1.pchome.tw',
'ms1.pchome.com.tw',
]
print("\n1⃣ 測試 IMAP 伺服器(接收郵件)")
print("-" * 80)
found_imap = None
for imap_host in imap_servers:
if test_imap_server(imap_host):
found_imap = imap_host
break
if not found_imap:
print("\n❌ 找不到可用的 IMAP 伺服器")
print("\n這可能表示:")
print(" 1. PChome 郵件伺服器只能在內部網路訪問")
print(" 2. 需要連接 VPN")
print(" 3. 伺服器名稱不是常見格式")
return
print(f"\n✅ 找到 IMAP 伺服器: {found_imap}")
# 根據 IMAP 推測 SMTP
print("\n2⃣ 根據 IMAP 推測 SMTP 伺服器(發送郵件)")
print("-" * 80)
smtp_guesses = guess_smtp_from_imap(found_imap)
smtp_ports = [587, 25, 465]
found_smtp = None
for smtp_host in smtp_guesses:
print(f"\n嘗試: {smtp_host}")
for port in smtp_ports:
if test_smtp_server(smtp_host, port):
found_smtp = (smtp_host, port)
break
if found_smtp:
break
print("\n" + "=" * 80)
print("📊 測試結果")
print("=" * 80)
if found_smtp:
print(f"\n🎉 找到可用的 SMTP 設定!")
print(f"\n建議的 .env 設定:")
print(f" EMAIL_HOST={found_smtp[0]}")
print(f" EMAIL_PORT={found_smtp[1]}")
print(f" EMAIL_HOST_USER=yingpin_chen@pchome.tw")
print(f" EMAIL_HOST_PASSWORD=您的郵件密碼")
print(f" EMAIL_SENDER=yingpin_chen@pchome.tw")
else:
print(f"\n⚠️ 找到 IMAP 但找不到 SMTP")
print(f"\n建議:")
print(f" 1. 檢查 Outlook/郵件客戶端的「寄件伺服器」設定")
print(f" 2. 詢問 IT 部門 SMTP 伺服器地址")
print(f" 3. 或暫時使用 Gmail 進行測試")
if __name__ == '__main__':
main()