Update main.py
Browse files
main.py
CHANGED
|
@@ -34,7 +34,8 @@ BOSS_LINE_ID = os.getenv("BOSS_LINE_ID", "")
|
|
| 34 |
# 🌟 LINE Pay 金鑰設定 🌟
|
| 35 |
LINE_PAY_CHANNEL_ID = os.getenv("LINE_PAY_CHANNEL_ID", "")
|
| 36 |
LINE_PAY_CHANNEL_SECRET = os.getenv("LINE_PAY_CHANNEL_SECRET", "")
|
| 37 |
-
LINE_PAY_BASE_URL = "https://sandbox-api-pay.line.me"
|
|
|
|
| 38 |
|
| 39 |
# 設定結帳後要跳回前端哪裡? (指向您的 GitHub Pages booking.html)
|
| 40 |
RETURN_URL = "https://ciecietaipei.github.io/booking.html"
|
|
@@ -343,42 +344,19 @@ class EmailPayload(BaseModel):
|
|
| 343 |
|
| 344 |
@app.post("/api/send_email")
|
| 345 |
async def send_email(payload: EmailPayload):
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
讓 ADMIN 可以直接呼叫 API 發送確認信
|
| 349 |
-
"""
|
| 350 |
-
import smtplib
|
| 351 |
-
from email.mime.text import MIMEText
|
| 352 |
-
from email.mime.multipart import MIMEMultipart
|
| 353 |
-
|
| 354 |
-
# 從環境變數讀取 Gmail 設定
|
| 355 |
-
GMAIL_USER = os.getenv("GMAIL_USER") # 你的 Gmail 地址
|
| 356 |
-
GMAIL_APP_PASSWORD = os.getenv("GMAIL_APP_PASSWORD") # Gmail 應用程式密碼
|
| 357 |
-
|
| 358 |
-
if not GMAIL_USER or not GMAIL_APP_PASSWORD:
|
| 359 |
-
raise HTTPException(status_code=500, detail="郵件服務未設定 (缺少 GMAIL_USER 或 GMAIL_APP_PASSWORD)")
|
| 360 |
-
|
| 361 |
try:
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
# 連接 Gmail SMTP 伺服器
|
| 373 |
-
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
|
| 374 |
-
server.login(GMAIL_USER, GMAIL_APP_PASSWORD)
|
| 375 |
-
server.send_message(msg)
|
| 376 |
-
|
| 377 |
-
return {
|
| 378 |
-
"status": "success",
|
| 379 |
-
"message": f"郵件已成功發送至 {payload.to}"
|
| 380 |
-
}
|
| 381 |
-
|
| 382 |
except Exception as e:
|
| 383 |
raise HTTPException(status_code=500, detail=f"郵件發送失敗: {str(e)}")
|
| 384 |
|
|
|
|
| 34 |
# 🌟 LINE Pay 金鑰設定 🌟
|
| 35 |
LINE_PAY_CHANNEL_ID = os.getenv("LINE_PAY_CHANNEL_ID", "")
|
| 36 |
LINE_PAY_CHANNEL_SECRET = os.getenv("LINE_PAY_CHANNEL_SECRET", "")
|
| 37 |
+
LINE_PAY_BASE_URL = "https://sandbox-api-pay.line.me"
|
| 38 |
+
RESEND_API_KEY = os.getenv("RESEND_API_KEY", "")
|
| 39 |
|
| 40 |
# 設定結帳後要跳回前端哪裡? (指向您的 GitHub Pages booking.html)
|
| 41 |
RETURN_URL = "https://ciecietaipei.github.io/booking.html"
|
|
|
|
| 344 |
|
| 345 |
@app.post("/api/send_email")
|
| 346 |
async def send_email(payload: EmailPayload):
|
| 347 |
+
if not RESEND_API_KEY:
|
| 348 |
+
raise HTTPException(status_code=500, detail="郵件服務未設定 (缺少 RESEND_API_KEY)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
try:
|
| 350 |
+
response = requests.post(
|
| 351 |
+
"https://api.resend.com/emails",
|
| 352 |
+
headers={"Authorization": f"Bearer {RESEND_API_KEY}", "Content-Type": "application/json"},
|
| 353 |
+
json={"from": f"{payload.name} <onboarding@resend.dev>", "to": [payload.to], "subject": payload.subject, "html": payload.htmlBody},
|
| 354 |
+
timeout=15
|
| 355 |
+
)
|
| 356 |
+
if response.status_code in (200, 201):
|
| 357 |
+
return {"status": "success", "message": f"郵件已成功發送至 {payload.to}"}
|
| 358 |
+
else:
|
| 359 |
+
raise HTTPException(status_code=500, detail=f"Resend 錯誤: {response.text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
except Exception as e:
|
| 361 |
raise HTTPException(status_code=500, detail=f"郵件發送失敗: {str(e)}")
|
| 362 |
|