from flask import current_app from flask_mail import Mail, Message from threading import Thread mail = Mail() def send_async_email(app, msg): """异步发送邮件""" with app.app_context(): try: mail.send(msg) except Exception as e: print(f"邮件发送失败: {e}") def send_email(to, subject, template, **kwargs): """发送邮件""" app = current_app._get_current_object() msg = Message( subject=subject, recipients=[to], html=template, sender=current_app.config['MAIL_DEFAULT_SENDER'] ) # 异步发送 thr = Thread(target=send_async_email, args=[app, msg]) thr.start() return thr def send_verification_email(email, code, code_type): """发送验证码邮件""" type_map = { 1: '注册', 2: '登录', 3: '找回密码' } subject = f'【太白购物】{type_map.get(code_type, "验证")}验证码' html_template = f""" 验证码邮件

太白购物平台

您好!

您正在进行{type_map.get(code_type, "验证")}操作,验证码为:

{code}

验证码有效期为10分钟,请及时使用。

如果这不是您的操作,请忽略此邮件。

此邮件由系统自动发送,请勿回复。

© 2024 太白购物平台 版权所有

""" return send_email(email, subject, html_template)