Spaces:
Sleeping
Sleeping
| # email_manager.py | |
| import smtplib | |
| from email.mime.multipart import MIMEMultipart | |
| from email.mime.text import MIMEText | |
| from email.mime.application import MIMEApplication | |
| import os | |
| import config | |
| import logging | |
| logger = logging.getLogger(__name__) | |
| def send_feedback_email(user_nickname, tribe, comment, image_path=None): | |
| """將回饋寄送到公務信箱 (使用 SSL Port 465)""" | |
| if not config.SENDER_EMAIL or not config.SENDER_PASSWORD: | |
| logger.error("❌ 寄信失敗:未設定 SENDER_EMAIL 或 SENDER_PASSWORD") | |
| return False | |
| try: | |
| msg = MIMEMultipart() | |
| msg['From'] = config.SENDER_EMAIL | |
| msg['To'] = config.RECEIVER_EMAIL | |
| msg['Subject'] = f"🚨 【族語 AI 報錯】來自 {user_nickname} 的回饋 ({tribe}語)" | |
| body = f""" | |
| <html> | |
| <body> | |
| <h2 style='color: #8B0000;'>收到新的使用者回饋</h2> | |
| <hr> | |
| <p><b>👤 使用者暱稱:</b> {user_nickname}</p> | |
| <p><b>🌍 使用族語:</b> {tribe}語</p> | |
| <p><b>💬 建議內容:</b> {comment}</p> | |
| <hr> | |
| <p style='color: gray; font-size: 12px;'>本郵件由 16族智慧翻譯小幫手自動發送</p> | |
| </body> | |
| </html> | |
| """ | |
| msg.attach(MIMEText(body, 'html')) | |
| if image_path and os.path.exists(image_path): | |
| with open(image_path, "rb") as f: | |
| part = MIMEApplication(f.read(), Name=os.path.basename(image_path)) | |
| part['Content-Disposition'] = f'attachment; filename="{os.path.basename(image_path)}"' | |
| msg.attach(part) | |
| # 💡 重點:改用 SMTP_SSL 而不是 starttls() | |
| with smtplib.SMTP_SSL(config.SMTP_SERVER, config.SMTP_PORT, timeout=15) as server: | |
| server.login(config.SENDER_EMAIL, config.SENDER_PASSWORD) | |
| server.send_message(msg) | |
| logger.info("✅ 郵件透過 SSL 寄送成功!") | |
| return True | |
| except Exception as e: | |
| logger.error(f"❌ 郵件發送失敗: {e}") | |
| return False |