经常我们登陆一些网站或者软件都会出现短信验证或者邮箱验证,今天来初步了解一下邮箱验证的实现。
路由配置等这里不再介绍,具体的邮箱设置请参考我以前的博客https://blog.youkuaiyun.com/qq_38059635/article/details/81569081
设置邮箱以及获得授权码
from django.shortcuts import render
from django.http import HttpResponse
from django.conf import settings
# 引入发送邮件的模块
from django.core.mail import send_mail,send_mass_mail,EmailMultiAlternatives
# 这里从setting里面引入了已经设置好了的
# # 设置邮件域名
# EMAIL_HOST = 'smtp.qq.com'
# # 设置端口号 (数字)
# EMAIL_PORT = 25
# # 邮箱以及授权码
# EMAIL_HOST_USER = '146*****684@qq.com'
# EMAIL_HOST_PASSWORD='reh*******gfdf'
# # 是否启用安全连接
# EMAIL_USER_TLS=True
# Create your views here.
def sendEmail(request):
return render(request,'index.html')
#单人单封发送
def send_Email(request):
# return HttpResponse('sendEmail seccess')
if request.method == 'POST':
# 邮件标题 邮件内容 发件人 收件人
res=send_mail('人生啊,生活啊,年轻啊','不要重视房价,要重视放假{}如是说'.format('罗老师'),
settings.EMAIL_HOST_USER,[settings.EMAIL_HOST_USER,])
if res ==1:
return HttpResponse('邮件发送成功')
else:
return HttpResponse('邮件发送失败')
else:
return render(request,'index.html')
#单人对多封发送
def send_mess_email(request):
message1=('物流信息','您的快递已送达',settings.EMAIL_HOST_USER,[settings.EMAIL_HOST_USER],)
message2=('物流详情','您的快递以被签收了',settings.EMAIL_HOST_USER,[settings,EMAIL_HOST_USER,])
res=send_mess_email((message1,message2))
if res==2:
return HttpResponse('多封邮件发送成功')
else:
return HttpResponse('多封邮件发送失败')
#发送html邮件 使用EmailMultiAlternatives
def send_html(request):
html_message='<a href="http://www.baidu.com">百度</a>'
res=EmailMultiAlternatives('商品链接','请点击下面的链接'+html_message,settings.EMAIL_HOST_USER,[settings.EMAIL_HOST_USER,])
res.content_subtype='html'
res.send()
return HttpResponse(res)