摘要:settings.py EMAIL_HOST = \"smtp.126.com\" EMAIL_POSRT = 25 EMAIL_HOST_USER = \"\"#邮箱名 EMAIL_HOST_PASSWORD = \"\"#密码 EMAIL_USE_TLS = False EMAIL_FROM = \"\"#邮箱名 建立utils文件夹新建send_email.py 生成随机数作为激活链接 f
settings.py
EMAIL_HOST = "smtp.126.com" EMAIL_POSRT = 25 EMAIL_HOST_USER = ""#邮箱名 EMAIL_HOST_PASSWORD = ""#密码 EMAIL_USE_TLS = False EMAIL_FROM = ""#邮箱名
建立utils文件夹新建send_email.py
from random import Random
def generate_random_str(randomlength=8): str = \'\' chars = \'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789\' length = len(chars)-1 random = Random() for i in range(randomlength): str+=chars[random.randint(0,length)] return str
发送邮件
from xxx.settings import EMAIL_FROM#导入smtp
from users.models import EmailVerifyRecord #导入模型
from django.core.mail import send_mail #导入发送邮件
def send_register_email(email,send_type="register"): email_record = EmailVerifyRecord()#实例化 random_str = generate_random_str(16)#取随机数 email_record.code = random_str email_record.email = email email_record.send_type = send_type email_record.save() if send_type == "register": email_title = "注册激活链接" email_body = "请点击下面的链接激活你的账号:http://127.0.0.1:8000/active/{0}".format(random_str) send_status = send_mail(email_title,email_body,EMAIL_FROM,[email]) if send_status: pass
app.views.py
from utils.email_send import send_register_email
from .forms import LoginForm
class RegisterView(View): def get(self,request):#访问 return render(request,"register.html") def post(self,request):#登陆 register_form = RegisterForm(request.POST) if register_form.is_valid():#表单验证 email = request.POST.get("email", "")
if UserProfile.objects.filter(email = email): return render(request, "register.html",{"register_form":register_form,"msg": "用户已经存在"}) pass_word = request.POST.get("password", "") user_profile = UserProfile()#实例化用户数据库 user_profile.username = email user_profile.email = email user_profile.is_active = False#邮箱验证状态 user_profile.password = make_password(pass_word)#密码生成 user_profile.save()#保存数据 send_register_email(email,"register")#发送邮件 return render(request,"login.html") else: return render(request, "register.html",{"register_form":register_form})
激活用户邮箱:.
app.views.py登陆视图
class LoginView(View): def get(self,request): return render(request, "login.html", {}) def post(self,request): login_form = LoginForm(request.POST) if login_form.is_valid():#验证表单填写信息是否有效 user_name = request.POST.get("username", "") pass_word = request.POST.get("password", "") user = authenticate(username=user_name, password=pass_word) if user is not None:#判断是否匹配 if user.is_active:#判断用户是否已激活 login(request, user)#登陆 return render(request, "index.html") else: return render(request, "login.html", {"msg": "邮箱未激活"})#未激活提示 else: return render(request, "login.html", {"msg": "用户名或密码错误"})#错误提示 else: return render(request,"login.html",{"login_form":login_form})#表单提示
app.views.py激活
#激活用户邮箱 class ActiveUserView(View): def get(self, request,active_code): all_records = EmailVerifyRecord.objects.filter(code=active_code) if all_records: for record in all_records: email = record.email user = UserProfile.objects.get(email=email) user.is_active = True user.save() return HttpResponse("The param is : " + active_code)
相关文章推荐
已有家长中招!“录取通知书”邮件暗藏骗局2022-09-06
连接企微和钉钉,新网全球邮助力企业协同办公2022-09-27
快速申请搭建免费企业邮箱的教程2022-09-16
如何避免你的外贸开发信被当成垃圾邮件?2022-09-14
2021年,全国企业邮箱用户共收发邮件约7637.7亿封2022-09-13