65 lines
2.3 KiB
Python
65 lines
2.3 KiB
Python
import smtplib
|
|
from email.mime.text import MIMEText
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.header import Header
|
|
import config
|
|
import traceback
|
|
|
|
|
|
def send_verification_email(recipient_email, verification_code):
|
|
"""发送验证邮件"""
|
|
print(f"尝试向 {recipient_email} 发送验证码: {verification_code}")
|
|
|
|
# 创建邮件对象
|
|
message = MIMEMultipart()
|
|
message['From'] = f"{config.EMAIL_FROM_NAME} <{config.EMAIL_FROM}>"
|
|
message['To'] = recipient_email
|
|
message['Subject'] = Header('验证您的文本分类系统账户', 'utf-8')
|
|
|
|
# 邮件正文
|
|
html_content = f"""
|
|
<html>
|
|
<body>
|
|
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
|
|
<h2>欢迎注册文本分类系统</h2>
|
|
<p>感谢您注册我们的文本分类系统。请使用下面的验证码完成注册流程:</p>
|
|
<div style="background-color: #f5f5f5; padding: 15px; font-size: 24px; text-align: center; letter-spacing: 5px; font-weight: bold; margin: 20px 0;">
|
|
{verification_code}
|
|
</div>
|
|
<p>此验证码将在30分钟内有效。</p>
|
|
<p>如果您没有注册此账户,请忽略此邮件。</p>
|
|
<p>谢谢,<br>文本分类系统团队</p>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
message.attach(MIMEText(html_content, 'html', 'utf-8'))
|
|
|
|
try:
|
|
print(f"连接到邮件服务器: {config.EMAIL_HOST}:{config.EMAIL_PORT}")
|
|
|
|
# 连接邮件服务器
|
|
smtp = smtplib.SMTP(config.EMAIL_HOST, config.EMAIL_PORT, timeout=10)
|
|
|
|
# 打印服务器响应
|
|
smtp.set_debuglevel(1)
|
|
|
|
print(f"开始TLS连接: {config.EMAIL_USE_TLS}")
|
|
if config.EMAIL_USE_TLS:
|
|
smtp.starttls()
|
|
|
|
print(f"尝试登录: {config.EMAIL_USERNAME}")
|
|
smtp.login(config.EMAIL_USERNAME, config.EMAIL_PASSWORD)
|
|
|
|
print(f"发送邮件从 {config.EMAIL_FROM} 到 {recipient_email}")
|
|
# 发送邮件
|
|
smtp.sendmail(config.EMAIL_FROM, recipient_email, message.as_string())
|
|
smtp.quit()
|
|
print("邮件发送成功")
|
|
return True
|
|
except Exception as e:
|
|
print(f"邮件发送失败: {e}")
|
|
print(traceback.format_exc()) # 打印完整的错误堆栈
|
|
return False
|