登录自己的QQ邮箱进入主页面
点击左上角的设置 --> 账户
找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
开启‘POP3/SMTP服务 (如何使用 Foxmail 等软件收发邮件?)’进行手机验证
记住授权码
在django的settings里配置
邮箱登录密码填刚才记住的授权码
视图Views:
利用到redis的缓存,存储验证码
from rest_framework.views import APIView
from rest_framework.response import Response
from .sers import *
import hashlib
import random
import string
import redis
from django.core.mail import send_mailrd = redis.Redis(port=6379,host='localhost')class RegisterView(APIView):def post(self,request):name = request.data.get('name')pwd = request.data.get('passwd')passwd = hashlib.md5()passwd.update(pwd.encode('utf-8'))passwd.hexdigest().upper()ve_code = request.data.get('ve_code')email = request.data.get('email')vcode = rd.get(email)if not vcode:return Response({'msg':'验证码失效'})user = User.objects.filter(name=name,passwd=passwd).first()if user:return Response({'msg':'用户已存在,请直接登陆'})vcode = vcode.decode()if vcode == ve_code:data = {'name':name,'email':email,'passwd':passwd,}User.objects.create(name=name,passwd=passwd,email=email)return Response({'msg':'注册成功','code':200})else:return Response({'msg':'注册失败','code':500})# 发送邮箱验证码
class Send_email(APIView):def post(self,request):email = request.data.get('email')try:capta = ''words = ''.join((string.digits))for i in range(6):capta += random.choice(words)rd.set(email,capta)rd.expire(capta,60*5)send_mail(subject='注册邮件',message='注册验证码!!!!!',from_email='xxxxxxxx@qq.com',recipient_list=[email],html_message='注册验证码:{}'.format(capta))return Response({'msg':'验证码已发送','code':200})except Exception as e:return Response({'msg':'验证码发送失败','code':400})
发送验证码即可进行注册啦~~