feat: 优化email code 发送
This commit is contained in:
parent
dbdc0086d2
commit
fb3edfa7a2
|
|
@ -4,6 +4,7 @@ from rest_framework.exceptions import ParseError
|
||||||
from apps.auth1.serializers import EmailCodeSerializer
|
from apps.auth1.serializers import EmailCodeSerializer
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from apps.utils.tools import rannum
|
from apps.utils.tools import rannum
|
||||||
|
from server.settings import get_sysconfig
|
||||||
|
|
||||||
|
|
||||||
class EmailCodeView(CreateAPIView):
|
class EmailCodeView(CreateAPIView):
|
||||||
|
|
@ -30,12 +31,22 @@ class EmailCodeView(CreateAPIView):
|
||||||
# 缓存验证码,有效期5分钟
|
# 缓存验证码,有效期5分钟
|
||||||
cache.set(cache_key, code, 60 * 5)
|
cache.set(cache_key, code, 60 * 5)
|
||||||
|
|
||||||
# 使用 send_mail_task 发送验证码邮件
|
# 使用 MyThread 发送验证码邮件
|
||||||
from apps.utils.tasks import send_mail_task
|
from django.core.mail import send_mail
|
||||||
send_mail_task.delay(
|
from apps.utils.thread import MyThread
|
||||||
subject='登录验证码',
|
|
||||||
|
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分钟内有效。如非本人操作,请忽略此邮件。',
|
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': '验证码已发送'})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue