117 lines
3.6 KiB
Python
117 lines
3.6 KiB
Python
import smtplib
|
||
import random
|
||
import string
|
||
from email.mime.text import MIMEText
|
||
from email.mime.multipart import MIMEMultipart
|
||
from flask import current_app, session
|
||
from app import db
|
||
from app.models import VerificationCode
|
||
|
||
|
||
# 邮件配置
|
||
EMAIL_CONFIG = {
|
||
'host': 'mail.sq0715.com',
|
||
'port': 587,
|
||
'username': 'sumkim@sq0715.com',
|
||
'password': 'sumkim0715',
|
||
'from_email': 'sumkim@sq0715.com',
|
||
'from_name': 'QINAI_OFFICIAL'
|
||
}
|
||
|
||
|
||
def generate_verification_code(length=6):
|
||
"""生成6位数字验证码"""
|
||
return ''.join(random.choices(string.digits, k=length))
|
||
|
||
|
||
def save_verification_code(email, code):
|
||
"""保存验证码到数据库"""
|
||
# 删除之前的验证码
|
||
VerificationCode.query.filter_by(email=email).delete()
|
||
db.session.commit()
|
||
|
||
# 创建新验证码记录
|
||
verification = VerificationCode(email=email, code=code)
|
||
db.session.add(verification)
|
||
db.session.commit()
|
||
return verification
|
||
|
||
|
||
|
||
def verify_code(email, code):
|
||
"""验证验证码是否正确且在有效期内"""
|
||
verification = VerificationCode.query.filter_by(
|
||
email=email,
|
||
code=code,
|
||
is_used=False
|
||
).first()
|
||
|
||
if not verification:
|
||
return False
|
||
|
||
# 验证成功后标记为已使用
|
||
verification.is_used = True
|
||
db.session.commit()
|
||
return True
|
||
|
||
|
||
def send_verification_email(to_email, code):
|
||
"""发送验证码邮件"""
|
||
subject = "【高可用学习平台】您的注册验证码"
|
||
|
||
# 创建邮件正文,支持HTML格式
|
||
html_content = f"""
|
||
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #eee; border-radius: 10px; background-color: #f9f9f9;">
|
||
<h2 style="color: #4a89dc; text-align: center;">高可用学习平台 - 邮箱验证</h2>
|
||
<p>您好,</p>
|
||
<p>感谢您注册高可用学习平台。请使用以下验证码完成注册:</p>
|
||
<div style="background-color: #4a89dc; color: white; font-size: 24px; font-weight: bold; text-align: center; padding: 15px; border-radius: 5px; letter-spacing: 5px; margin: 20px 0;">
|
||
{code}
|
||
</div>
|
||
<p>验证码有效期为10分钟,请尽快完成注册。</p>
|
||
<p>如果您没有进行注册操作,请忽略此邮件。</p>
|
||
<p style="margin-top: 30px; padding-top: 10px; border-top: 1px solid #eee; font-size: 12px; color: #666;">
|
||
此邮件由系统自动发送,请勿直接回复。
|
||
</p>
|
||
</div>
|
||
"""
|
||
|
||
plain_text = f"""
|
||
高可用学习平台 - 邮箱验证
|
||
|
||
您好,
|
||
|
||
感谢您注册高可用学习平台。请使用以下验证码完成注册:
|
||
|
||
{code}
|
||
|
||
验证码有效期为10分钟,请尽快完成注册。
|
||
|
||
如果您没有进行注册操作,请忽略此邮件。
|
||
|
||
此邮件由系统自动发送,请勿直接回复。
|
||
"""
|
||
|
||
# 创建MIMEMultipart对象
|
||
message = MIMEMultipart("alternative")
|
||
message["Subject"] = subject
|
||
message["From"] = f"{EMAIL_CONFIG['from_name']} <{EMAIL_CONFIG['from_email']}>"
|
||
message["To"] = to_email
|
||
|
||
# 添加文本和HTML版本
|
||
message.attach(MIMEText(plain_text, "plain"))
|
||
message.attach(MIMEText(html_content, "html"))
|
||
|
||
try:
|
||
# 连接到SMTP服务器
|
||
server = smtplib.SMTP(EMAIL_CONFIG['host'], EMAIL_CONFIG['port'])
|
||
server.starttls() # 启用TLS加密
|
||
server.login(EMAIL_CONFIG['username'], EMAIL_CONFIG['password'])
|
||
|
||
# 发送邮件
|
||
server.sendmail(EMAIL_CONFIG['from_email'], to_email, message.as_string())
|
||
server.quit()
|
||
return True
|
||
except Exception as e:
|
||
print(f"发送邮件失败: {str(e)}")
|
||
return False |