1. 邮箱设置----->账户——>POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务下开启
# 发送邮箱验证码
EMAIL_HOST = "smtp.qq.com" # 服务器
EMAIL_PORT = 25 # 一般情况下都为25
EMAIL_HOST_USER = "qq账号@qq.com" # 账号
EMAIL_HOST_PASSWORD = "授权码" # (上面保存的授权码)
EMAIL_USE_TLS = True # 一般都为False
EMAIL_FROM = "qq账号@qq.com" # 邮箱来自
email_title = '邮箱激活'
from django.core.mail import send_mail
def send_sms_code(to_email):
"""
发送邮箱验证码
:param to_mail: 发到这个邮箱
:return: 成功:0 失败 -1
"""
# 生成邮箱验证码
sms_code = '%06d' % random.randint(0, 999999)
EMAIL_FROM = "[email protected]" # 邮箱来自
email_title = '邮箱激活'
email_body = "您的邮箱注册验证码为:{0}, 该验证码有效时间为两分钟,请及时进行验证。".format(sms_code)
send_status = send_mail(email_title, email_body, EMAIL_FROM, [to_email])
return send_status
只要传递一个邮箱账号,你的邮箱就会给他会送邮件了,
因为他是个异步操作,他不执行完,下面的就要等着,因此我们要最好使用celery把这个任务交给celery去执行,也就是django程序正常运行,执行到发邮件的时候,让celery去做,然后django本身接着执行下一步操作
celery的使用
from django.core.mail import send_mail
@celery_app.task(name='send_sms_code')
def send_sms_code(to_email):
"""
发送邮箱验证码
:param to_mail: 发到这个邮箱
:return: 成功:0 失败 -1
"""
# 生成邮箱验证码
sms_code = '%06d' % random.randint(0, 999999)
EMAIL_FROM = "[email protected]" # 邮箱来自
email_title = '邮箱激活'
email_body = "您的邮箱注册验证码为:{0}, 该验证码有效时间为两分钟,请及时进行验证。".format(sms_code)
send_status = send_mail(email_title, email_body, EMAIL_FROM, [to_email])
return send_status
在函数上面加上一句@celery_app.task(name='send_sms_code')
让celery程序识别就好
使用时:send_sms_code.delay()
如果想要激活,改变的是一个状态,那么一个字段值的布尔值可以满足,例如:把email_active默认的False改为True
改变的这个布尔值属于哪个模型?就要传入这个模型的对象,同时还有这个状态值
例如:{‘user_id’: user.id, ‘email’: user.email}
我们接受的只是一个邮箱,好像只能利用url地址的参数拼接上去,然后把想要的信息取下来
url拼接地址本身不安全,可以利用itsdangerous进行加密,然后再对取下来的信息解密
itsdangerous的使用
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings
def generate_verify_email_url(user):
"""
生成邮箱激活链接
:param user: 当前登录用户
:return: token
"""
EMAIL_VERIFY_URL = 'http://127.0.0.1:8000/emails/verification/'
s = Serializer(settings.SECRET_KEY, 60 * 60 * 24)
data = {'user_id': user.id, 'email': user.email}
token = s.dumps(data)
print(type(token))
return EMAIL_VERIFY_URL + '?token=' + token.decode()
def check_verify_email_token(token):
"""
反序列化token,获取到user
:param token:
:return:
"""
s = Serializer(settings.SECRET_KEY, 60 * 60 * 24)
try:
data = s.loads(token)
except BadData:
return None
else:
# 从data中取出user_id和email
user_id = data.get('user_id')
email = data.get('email')
try:
user = User.objects.get(id=user_id, email=email)
except User.DoesNotExist:
return None
else:
return use
使用celery的发邮件功能
@celery_app.task(name='send_vervify_email')
def send_vervify_email(self, to_email, verify_url):
"""定义发送验证邮件的任务"""
# send_mail(‘标题’,'普通邮件正文','发件人','收件人列表','富文本邮件正文(html)')
subject = '美多商城邮箱验证'
html_message = '最劲的用户您好
' \
'感谢您使用美多商城
' \
'您的邮箱为:%s。请点击此连接激活您的邮箱
' \
'' % (to_email, verify_url, verify_url)
try:
send_mail(subject, '', settings.EMAIL_FROM, [to_email], html_message=html_message)
# send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
except Exception as e:
print(e)
class EmailView(LoginLoginRequiredJSONMixin,View):
"""添加邮箱"""
def put(self, request):
# 接收参数
# body 类型是bytes
json_str = request.body.decode()
json_dict = json.loads(json_str) # 将json转化成字典
email = json_dict.get('email')
# 校验参数
if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email):
return http.HttpResponseForbidden('邮箱错误')
# 将用户传入的邮箱保存到用户数据库的email字段中
try:
request.user.email = 'email'
request.user.save()
except Exception as e:
logger.error(e)
return http.JsonResponse({'code': 200, 'errmsg': '添加邮箱失败'})
# 发送邮件验证邮件
verify_url = generate_verify_email_url(request.user)
print(verify_url)
send_vervify_email.delay(email, verify_url)
# 响应结果
return http.JsonResponse({'code': 200, 'errmsg': 'OK'})
class VerifyEmailView(View):
"""验证邮箱"""
def get(self, request):
# 接收参数
token = request.GET.get('token')
# 校验参数
if not token:
return http.HttpResponseForbidden('缺少必传参数')
# 从token中提取用户信息user_id ==> user
user = check_verify_email_token(token)
if not user:
return http.HttpResponseBadRequest('无效的token')
# 将用户的email_active 字段设置为True
try:
user.email_active = True
user.save()
except Exception as e:
logger.error(e)
return http.HttpResponseServerError('激活邮箱失败')
# 响应结果:重定向到用户中心
return redirect('users:info')