這是一個自動從 RSS feeds 獲取新聞並通過電子郵件發送的 Python 程式。
- 從多個新聞來源獲取 RSS feeds
- 自動過濾指定時間範圍內的新聞
- 生成美觀的 HTML 格式郵件
- 支援時區設定
- 完整的日誌記錄
pip3 install feedparser python-dotenv pytz
- 複製
.env.example
為.env
並填入您的設定:
cp .env.example .env
- 編輯
.env
文件,填入以下資訊:
# 電子郵件設定
EMAIL=your_email@gmail.com
EMAIL_PASSWORD=your_app_password
RECIPIENT_EMAIL=recipient@example.com
# SMTP 伺服器設定 (Gmail 範例)
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
# 時區設定
TIMEZONE=Asia/Taipei
如果您使用 Gmail,需要:
- 啟用兩步驟驗證
- 生成應用程式密碼
- 使用應用程式密碼作為
EMAIL_PASSWORD
python3 news_email_agent.py
from news_email_agent import NewsEmailAgent
agent = NewsEmailAgent()
agent.run(hours_back=24) # 獲取過去24小時的新聞
- BBC News
- CNN
- Reuters
- NPR
您可以在 NewsEmailAgent
類別中修改 news_feeds
列表來添加或移除新聞來源。
程式會生成 news_agent.log
文件記錄運行狀態和錯誤資訊。
- 確保網路連線正常
- 檢查 SMTP 設定是否正確
- 某些新聞來源可能需要 VPN 才能訪問