邮件和短信发送:通知的作用,验证的作用。
验证:比如用户密码相关,资金相关,都是对身份的验证。
会使用邮箱或者短信钉钉做一个登陆验证(获取验证码,添加验证码,登陆)
获取验证码:页面增加按钮,ajax请求,视图可以使用get请求。
提交验证码:随着form表单提交,登陆接口视图,接收验证码进行验证
1、python邮件发送
python发送邮件方便,python中有内置模块:
- smtplib
- smtp协议,python发送邮件默认基于轻量级的邮件协议,smtp是一个通过发送地址和目的地址进行邮件发送的邮件协议。
- 整合邮件内容,及邮件内容的格式
- 发送邮件:
- 邮件 : 发件人 收件人 内容(文本、附件、html(自动化测试报告)) 主体
- 邮件服务器 :登录服务器 登录账号 发送邮件 退出服务器
使用163邮箱进行发送邮件
开启163服务
开启服务
imap 发送邮件
pop3 接收邮件
163邮箱服务端口
发送邮件
import smtplib
from email.mime.text import MIMEText
#构建邮件
#主题
subject = "邮件测试"
#发送内容
content = "GOOD DAY"
#发送人
sender = "str_gym@163.com"
#接收人 单个 多个收件人
rec = """231312@qq.com,
**********,
*********,
**********
"""
password = "123456"
#MIMEText 参数 发送内容 内容类型 编码
message = MIMEText(content,"plain","utf-8")
message["Subject"] = subject
message["From"] = sender ##发件人
message["To"] = rec ##收件人
##发送邮件
smtp = smtplib.SMTP_SSL("smtp.163.com",465)
smtp.login(sender,password)
##参数说明 发件人 收件人需要一个列表 发送邮件 类似一种json的格式
smtp.sendmail(sender,rec,split(",\n"),message.as_string())
smtp.close()
2、python短信发送
常用的:阿里短信,云通讯
注册
import requests ### http库 pip install requests
##请求地址
url = "http://106.ihuyi.com/webservice/sms.php?method=Submit"
#APIID
accout = "c53007282"
#APIKEY
password = "62aa30a7402d2ffa127b33e7e94e7a0f"
##收件人手机号
mobile = "13780493295"
##短信内容
content = "您的验证码是:1111。请不要把验证码泄露给其他人。"
##请求头
headers = {
"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"
}
##构建发送参数
data = {
"account":account,
"password":password,
"mobile":mobile,
"content":content,
}
##发送
response = request.post(url,headers = headers,data = data)
#url 请求地址
#headers 请求头
#data 请求数据 内容
print(response.content.decode())
3、钉钉通信机器人
4.Django邮件发送与短信发送
web校验流程
登录
-
get请求,获取form表单
-
填写数据 邮箱地址,密码,验证码
-
视图接收参数 邮箱地址 密码 验证码
-
判断
-
判断用户是否存在
-
判断密码是否正确
-
校验验证码
-
保存验证码
-
-
-
-
返回登录结果
-
用户登录页面
-
获取验证码
-
视图发送获取验证码的请求
-
保存验证码 到数据库(mysql,sqllit3, reids() 提供生命周期)
-
过期时间
-
用户
-
页面增加button 使用ajax获取验证码 get请求
-
-
输入验证码
-
输入用户名 密码
-
提交
-
验证信息
-
验证码 校验
-
验证码字符串是否一致
-
时间
-
用户
-
状态 (使用或者未使用)
-
-
创建模型
验证码 用户 时间 状态
class Vaild_Code(models.Model):
code_content = models.CharField(max_length=8,verbose_name="验证码")
code_time = models.DateTimeField(auto_now=True,verbose_name="创建时间")
code_status = models.IntegerField(verbose_name="状态") ##1 使用 0 未使用
code_user = models.EmailField(verbose_name="邮箱")
使用邮件发送验证码
import smtplib
from email.mime.text import MIMEText
def send_email(params):
## params 字典类型
"""
:param params: 字典
subject 主题
content 邮件内容
toemail 收件人 列表
:return:
"""
subject = params.get("subject")
content = params.get("content")
sender = "str_wjp@163.com"
rec = params.get("toemail")
password = "qaz123"
message = MIMEText(content,"plain","utf-8")
message["Subject"] = subject
message["From"] = sender ## 发件人
message["To"] = str(rec) ## 收件人
try:
smtp = smtplib.SMTP_SSL("smtp.163.com",465)
smtp.login(sender,password)
smtp.sendmail(sender,rec,message.as_string())
smtp.close()
return True
except:
return False
import random
def get_code(request):
result = {"code":10000,"msg":""}
## 获取email
email = request.GET.get("email")
print(email)
## 判断用户是否存在
if email:
## 判断 email 是否有值
flag = LoginUser.objects.filter(email = email).exists()
if flag:
## 用户存在
##发送验证码
##
code = random.randint(1000,9999) ## 4位
content = "您的验证码是%s,打死不要告诉别人"% (code)
params = dict(subject="登录验证码",content = content,toemail=[email])
eflag = send_email(params)
if eflag:
## 保存验证码到数据库
vaile_code = Vaild_Code()
vaile_code.code_content = code
vaile_code.code_status = 0
vaile_code.code_user = email
vaile_code.save()
result = {"code": 10000, "msg": "验证码发送成功"}
else:
result = {"code": 10003, "msg": "未知错误,联系客服"}
else:
## 用户不存在
result = {"code": 10002, "msg": "用户不存在"}
else:
result = {"code": 10001, "msg": "邮箱不能为空"}
return JsonResponse(result)
增加验证码校验
import datetime
import time
def login(request):
if request.method == "POST":
error_msg = ""
email = request.POST.get("email")
password = request.POST.get("password")
code = request.POST.get("vaild_code")
if email:
user = LoginUser.objects.filter(email=email,user_type=0).first()
if user:
## 存在
if user.password == setPassword(password):
## 登录成功
## 跳转页面
# error_msg = "登录成功"
# return HttpResponseRedirect('/index/')
## 设置cookie
## 判断验证码 从库里取验证码
vaild_code = Vaild_Code.objects.filter(code_status=0,code_user=email,code_content=code).first()
## 判断时间 有效期2分钟 当前时间 - code创建时间 <= 2min
# now = time.time()
now = time.mktime(datetime.datetime.now().timetuple())
db_time = datetime.datetime.strptime(vaild_code.code_time, "%Y-%m-%d %H:%M:%S")
db_time = time.mktime(db_time.timetuple())
#
if (now - db_time)/ 60 > 2:
## 超时
error_msg = "验证码超时"
else:
response = HttpResponseRedirect("/Saller/index/")
response.set_cookie("username",user.username)
response.set_cookie("userid",user.id)
request.session['username'] = user.username ## 设置session
vaild_code.code_status = 1
vaild_code.save()
return response
else:
error_msg = "密码错误"
else:
error_msg = "用户不存在"
else:
error_msg = "邮箱不可以为空"
return render(request,"saller/login.html",locals())