feat: 优化email code 发送

This commit is contained in:
caoqianming 2026-03-09 12:52:41 +08:00
parent dbdc0086d2
commit fb3edfa7a2
1 changed files with 19 additions and 8 deletions

View File

@ -4,6 +4,7 @@ from rest_framework.exceptions import ParseError
from apps.auth1.serializers import EmailCodeSerializer
from django.core.cache import cache
from apps.utils.tools import rannum
from server.settings import get_sysconfig
class EmailCodeView(CreateAPIView):
@ -30,12 +31,22 @@ class EmailCodeView(CreateAPIView):
# 缓存验证码有效期5分钟
cache.set(cache_key, code, 60 * 5)
# 使用 send_mail_task 发送验证码邮件
from apps.utils.tasks import send_mail_task
send_mail_task.delay(
subject='登录验证码',
# 使用 MyThread 发送验证码邮件
from django.core.mail import send_mail
from apps.utils.thread import MyThread
def send_email():
config = get_sysconfig()
base_name_short = config['base']['base_name_short']
send_mail(
subject=f'{base_name_short}-验证码',
message=f'您的验证码是:{code}5分钟内有效。如非本人操作请忽略此邮件。',
recipient_list=[email]
from_email=None, # 使用默认发件人
recipient_list=[email],
fail_silently=False
)
return Response({'message': '验证码已发送'})
thread = MyThread(target=send_email)
thread.start_p()
return Response({'ok_msg': '验证码已发送'})