Files
2026-05-13 16:43:53 +08:00

44 lines
1.4 KiB
Python

import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_verification_email(to_email: str, token: str):
smtp_host = os.getenv("SMTP_HOST", "smtp.qq.com")
smtp_port = int(os.getenv("SMTP_PORT", "465"))
smtp_user = os.getenv("SMTP_USER", "")
smtp_password = os.getenv("SMTP_PASSWORD", "")
frontend_url = os.getenv("FRONTEND_URL", "http://localhost:5173")
if not smtp_user or not smtp_password:
print("SMTP configuration is missing. Skip sending email.")
return
msg = MIMEMultipart()
msg['From'] = smtp_user
msg['To'] = to_email
msg['Subject'] = "请验证你的邮箱地址"
verify_link = f"{frontend_url}/verify-email?token={token}"
body = f"""
<html>
<body>
<h2>欢迎使用全源灵动!</h2>
<p>请点击下方链接验证邮箱并激活账号:</p>
<p><a href="{verify_link}">{verify_link}</a></p>
<p>如果你没有发起该请求,请忽略此邮件。</p>
</body>
</html>
"""
msg.attach(MIMEText(body, 'html'))
try:
# Use SMTP_SSL for port 465
server = smtplib.SMTP_SSL(smtp_host, smtp_port)
server.login(smtp_user, smtp_password)
server.send_message(msg)
server.quit()
print(f"Verification email sent to {to_email}")
except Exception as e:
print(f"Failed to send email: {e}")