测试 获取验证码
import smtplib from email.mime.text import MIMEText from email.utils import formataddr #定义参数 my_mail = "2501186626@qq.com" #授权码 my_pass = "qnxrmxwxlyzoeadc" #定义发送邮件的方法 def mail(subject,content,mailaddr): #声明邮件对象 msg = MIMEText(content,'plain','utf-8') #设置发送方对象 msg['From'] = formataddr(['在线教育平台',my_mail]) #设置收件方对象 msg['To'] = formataddr(['尊敬的客户',mailaddr]) #设置标题 msg['Subject'] = subject #设置smtp服务器 server = smtplib.SMTP_SSL("smtp.qq.com",465) #登录邮箱 server.login(my_mail,my_pass) #发送邮件 server.sendmail(my_mail,[mailaddr],msg.as_string()) #关闭smtp链接 server.quit() mail('验证码验证服务','您的验证码是6666,有限期为2分钟','164850527@qq.com')
获取qq验证码接口
class Main(APIView): def post(self,request): my_mail=request.data.get('email') mailaddr=my_mail my_pass = "qnxrmxwxlyzoeadc" subject='验证码验证服务' import random content='%06d' % random.randint(0, 999999) # 随机6位验证码 msg = MIMEText(content, 'plain', 'utf-8') # 设置发送方对象 msg['From'] = formataddr(['在线教育平台', my_mail]) # 设置收件方对象 msg['To'] = formataddr(['尊敬的客户', mailaddr]) # 设置标题 msg['Subject'] = subject # 设置smtp服务器 server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 登录邮箱 server.login(my_mail, my_pass) # 发送邮件 server.sendmail(my_mail, [mailaddr], msg.as_string()) # 关闭smtp链接 server.quit() redis_client = get_redis_connection('default') # 指定设置 redis 库 redis_client.setex(my_mail, 60 * 5, content) # 通过键 和有效时间 存储验证码 return Response({'msg':'ok','code':'发送成功',})
setting 配置 redis 数据库
CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/1", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } } }
获取redis 验证码
from django_redis import get_redis_connection redis_client = get_redis_connection('default') # 指定数据库 redis_code=redis_client.get(email) # phone:code # 获取数据 if redis_code: redis_code= redis_code.decode() if not code ==redis_code: return Response({'msg':'验证码不正确'})