def send_password_email(email): email_record = EmailVerifyCode() code = ''.join(random.sample(string.ascii_letters + string.digits, 20)) # 注册获取16位长度的字符 email_record.code = code # 验证码 email_record.email = email # 邮箱 email_record.save() # 保存到数据库中 nick_name = UserProfile.objects.get(email=email).nick_name email_title = "{0} - 重置密码".format(nick_name) email_body = "请点击下面的链接重置你的密码:http://127.0.0.1:8000/password?code={0}".format(code) send_status = send_mail(email_title, email_body, EMAIL_FROM, [email]) return send_status
def send_register_email(email, send_type='register'): evf = EmailVerifyCode() if send_type == 'update_em': code = create_random_str(4) else: code = create_random_str(16) evf.email = email evf.code = code evf.send_type = send_type evf.save() mail_title = '' mail_body = '' if evf.send_type == 'register': mail_title = 'gbyang的网站的注册激活链接' mail_body = '请单击以下链接进行激活 http://127.0.0.1:8000/active/{}'.format(code) # 发送邮件 需要在setting进行相关配置 send_status = send_mail(mail_title, mail_body, EMAIL_FROM, [email]) if send_status: pass elif evf.send_type == 'forget': mail_title = 'gbyang的网站的密码重置链接' mail_body = '请单击以下链接进行密码重置 http://127.0.0.1:8000/reset/{}'.format(code) # 发送邮件 需要在setting进行相关配置 send_status = send_mail(mail_title, mail_body, EMAIL_FROM, [email]) if send_status: pass elif evf.send_type == 'update_em': mail_title = 'gbyang的网站的邮箱重置链接' mail_body = '您的验证码是: {}'.format(code) # 发送邮件 需要在setting进行相关配置 send_status = send_mail(mail_title, mail_body, EMAIL_FROM, [email]) if send_status: pass
def send_email_code(email,send_type): #第一步先存储验证码对象 code = get_random_code(8) email_ver = EmailVerifyCode() email_ver.email = email email_ver.send_type = send_type email_ver.code = code email_ver.save() #第二步,准备参数发送邮件 send_title = '' send_body = '' #根据发送类型不同,准备不同的参数 if send_type == 'register': send_title = '欢迎注册谷粒教育在线网站' send_body = '请点击以下链接进行激活:\n http://127.0.0.1:8000/users/user_active/'+code send_mail(send_title,send_body,EMAIL_FROM,[email]) if send_type == 'forget': send_title = '谷粒教育重置密码系统' send_body = '请点击以下链接进行重置密码:\n http://127.0.0.1:8000/users/user_resetpwd/'+code send_mail(send_title,send_body,EMAIL_FROM,[email]) if send_type == 'change': send_title = '谷粒教育重置邮箱系统' send_body = '您的验证码是:'+code send_mail(send_title,send_body,EMAIL_FROM,[email])
def send_email_code(email, send_type): # 第一步:创建邮箱验证码对象,保存数据库,用来以后做对比 code = get_random_code(8) a = EmailVerifyCode() a.email = email a.send_type = send_type a.code = code a.save() # 第二步:正式的发邮件功能 send_title = '' send_body = '' if send_type == 1: send_title = '欢迎注册谷粒教育网站' send_body = '请点击以下链接进行激活您的账号:\n http://127.0.0.1:8000/users/user_active/' + code send_mail(send_title, send_body, EMAIL_FROM, [email]) if send_type == 2: send_title = '谷粒教育重置密码' send_body = '请点击以下链接进行重置您的账号:\n http://127.0.0.1:8000/users/user_reset/' + code send_mail(send_title, send_body, EMAIL_FROM, [email]) if send_type == 3: send_title = '谷粒教育修改邮箱' send_body = '您的验证码是:' + code send_mail(send_title, send_body, EMAIL_FROM, [email])
def send_email_code(email, send_type): '''这是根据发送类型发送邮件的函数''' #第一步,创建验证码,存进数据库,用于比对 code = get_random_code(8) evc = EmailVerifyCode() evc.code = code evc.email = email evc.send_type = send_type evc.save() send_title = '' send_body = '' #第二步,根据发送类型发送邮件 if send_type == 1: send_title = '欢迎注册kkblog' send_body = '请点击下面的链接完成账号激活:\n' + 'http://127.0.0.1:8000/users/user_activate/' + code + '\n注意:链接有效时长为5分钟,激活完成会自动跳转到登录页面。' send_mail(send_title, send_body, EMAIL_FROM, [email]) if send_type == 2: send_title = 'kkblog重置密码' send_body = '请点击下面的链接重置密码:\n' + 'http://127.0.0.1:8000/users/reset/' + code + '\n注意:链接有效时长为5分钟,重置完成会自动跳转到登录页面。' send_mail(send_title, send_body, EMAIL_FROM, [email]) if send_type == 3: send_title = 'kkblog修改邮箱' send_body = '您此次修改邮箱的验证码是:\n' + code + '\n注意,验证码的有效时长是5分钟,验证完成会跳转到登录页面。' send_mail(send_title, send_body, EMAIL_FROM, [email])
def send_email_code( email, send_type, ): code = get_random_code(8) if send_type == 3: code = get_random_code(6) a = EmailVerifyCode() a.email = email a.send_type = send_type a.code = code a.save() send_title = '' send_body = '' if send_type == 1: send_title = '欢迎注册谷粒教育' send_body = '请点击以下链接进行激活:\n http://127.0.0.1:8000/users/user_active/' + code send_mail(send_title, send_body, EMAIL_FROM, [email]) if send_type == 2: send_title = '谷粒教育重置密码邮件' send_body = '谷粒教育重置密码邮件:\n http://127.0.0.1:8000/users/user_reset/' + code send_mail(send_title, send_body, EMAIL_FROM, [email]) if send_type == 3: send_title = '谷粒教育修改邮箱' send_body = '请查收您的验证码:' + code send_mail(send_title, send_body, EMAIL_FROM, [email])
def my_send_mail(email, send_type='register'): # 1、先生成一个验证码并保存到数据库,供后续激活使用 email_recode = EmailVerifyCode() email_recode.email = email email_recode.send_type = send_type code = generate_random_str(16) email_recode.code = code email_recode.save() # 2 组合一个注册邮件的激活信息 if send_type == 'register': email_title = '慕学网在线注册激活链接' email_body = '请点击下面的链接激活您的账号:http://127.0.0.1:8000/active/{0}'.format( code) send_status = send_mail(email_title, email_body, EMAIL_FROM, [email]) if send_status: return send_status else: return None if send_type == 'forget': email_title = '慕学网重置密码' email_body = '请点击下面的链接重置你的密码:http://127.0.0.1:8000/reset/{0}'.format( code) send_status = send_mail(email_title, email_body, EMAIL_FROM, [email]) if send_status: return send_status else: return None
def send_register_email(email, send_type='register'): email_record = EmailVerifyCode() # 一定要加()!!!!!!这个问题我搞了一天才发现!!! code = random_str(16) email_record.code = code 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( code) send_stats = send_mail(email_title, email_body, EMAIL_FROM, [email]) if send_stats: pass elif send_type == 'forget': email_title = "神童研究院密码重置系统" email_body = "请点击下方链接进入实验室重置密码空间:http://127.0.0.1:8000/reset/{0}".format( code) send_stats = send_mail(email_title, email_body, EMAIL_FROM, [email]) if send_stats: pass elif send_type == 'update_email': email_title = "神童研究院邮箱修改系统" email_body = "你的邮箱验证码为:{0}".format(code) send_stats = send_mail(email_title, email_body, EMAIL_FROM, [email]) if send_stats: pass
def send_to_email(email, send_type='register'): email_record = EmailVerifyCode() email_record.code = get_random_code() email_record.email = email email_record.send_type = send_type email_record.save() if send_type == 'register': title = 'Richard Tsoi博客在线注册链接' body = '点击链接激活账号:http://127.0.0.1:8000/user/active/{0}'.format( email_record.code) send_status = send_mail(title, body, settings.DEFAULT_FROM_EMAIL, [email]) if send_status: pass if send_type == 'reset': title = 'Richard Tsoi博客找回密码链接' body = '点击链接找回密码:http://127.0.0.1:8000/user/changepwd ,验证码为{0}'.format( email_record.code) send_status = send_mail(title, body, settings.DEFAULT_FROM_EMAIL, [email]) if send_status: pass
def send_mail_code(email, send_type): """ 创建邮箱验证码对象,保存到数据库,用来做对比 @params: email邮件发送对象 @params: send_type: 邮件发送类型 """ code = get_random_code(16) a = EmailVerifyCode() a.email = email a.send_type = send_type a.code = code a.save() # 发送邮件 if send_type == 1: subject = '[在线教育网]欢迎注册' message = '请点击以下链接激活您的账号(5分钟内有效):\nhttp://127.0.0.1:8000/users/user_active/{0}'.format( code) send_mail(subject, message, EMAIL_FROM, [email]) if send_type == 2: subject = '[在线教育网]重置密码' message = '请点击以下链接重置您的密码(5分钟内有效):\nhttp://127.0.0.1:8000/users/user_reset/{0}'.format( code) send_mail(subject, message, EMAIL_FROM, [email]) if send_type == 3: subject == '[在线教育网]更换邮箱' message = '您的邮箱验证码是(5分钟内有效):' + code send_mail(subject, message, EMAIL_FROM, [email])
def email_send(email, type='register'): # 邮件发送函数 code = random_str(16) email_record = EmailVerifyCode() email_record.code = code email_record.email = email email_record.send_type = type email_record.save() email_title = '' email_body = '' if type == 'register': # 注册 email_title = 'ss在线教育平台注册' email_body = '请点击链接进行激活: http://127.0.0.1:8000/active/{0}'.format(code) email_status = send_mail(email_title, email_body, DEFAULT_FROM_EMAIL, [email]) if email_status: pass elif type == 'forget': # 找回密码 email_title = 'ss在线教育平台密码招呼' email_body = '请点击链接进行密码找回: http://127.0.0.1:8000/reset/{0}'.format( code) email_status = send_mail(email_title, email_body, DEFAULT_FROM_EMAIL, [email]) if email_status: pass
def sendEmail(*,email,send_type): code = SendEmailUtil.getRandomCode(5) a = EmailVerifyCode() a.send_type = send_type a.email = email a.code = code a.save() recipient_list = [email] if send_type == 1: subject = '欢迎注册' message = '请点击以下链接进行激活,激活您的账号: \n http://127.0.0.1:8000/users/user_active/'+code # 发送邮件 send_mail(subject=subject,message=message,from_email = EMAIL_FROM,recipient_list = recipient_list)
def send_email_code(email, send_type): #1 创建邮箱验证码对象,保存数据库 code = get_ramdon_code(6) e = EmailVerifyCode() e.email = email e.send_type = send_type e.code = code e.save() # 保存验证码到数据库 #第二步:正式的发邮件功能 if send_type == 3: # 修改邮箱 send_title = '修改邮箱验证码' send_body = '你的验证码是' + code send_mail(send_title, send_body, settings.DEFAULT_FROM_EMAIL, [email])
def send_code_email(email_to): code = random_str() email_code = EmailVerifyCode() email_code.email = email_to email_code.verify_code = code email_code.send_type = 'update_email' email_code.save() subject = '邮箱修改验证码' message = '您的邮箱验证码为: {0}, 验证码在20分钟内有效'.format(code) send_status = send_mail(subject, message, EMAIL_FROM, [email_to]) if send_status: print('验证码邮件已发送') else: print('邮件发送失败')
def send_email(email, code_length=16): code = generate_random_str(code_length) email_verify = EmailVerifyCode() email_verify.code = code email_verify.email = email email_verify.save() email_title = "Password Reset" email_body = "Reset password: http://127.0.0.1/reset/{0}".format(code) send_status = send_mail(email_title, email_body, EMAIL_FROM, [email]) if send_status: pass
def send_password_email(email): email_record = EmailVerifyCode() code = ''.join(random.sample(string.ascii_letters + string.digits, 20)) # 注册获取16位长度的字符 email_record.code = code # 验证码 email_record.email = email # 邮箱 email_record.save() # 保存到数据库中 nick_name = UserProfile.objects.get(email=email).nick_name email_title = "{0} - 重置密码".format(nick_name) email_body = "请点击下面的链接重置你的密码:http://127.0.0.1:8000/password?code={0}".format( code) send_status = send_mail(email_title, email_body, EMAIL_FROM, [email]) return send_status
def send_register_mail(email, send_type="register"): # 发送之前先保存到数据库,到时候查询链接是否存在 # 实例化一个EmailVerifyCode对象 email_record = EmailVerifyCode() # 生成随机的code放入链接 if send_type == 'update_mail': code = random_str(4) else: code = random_str(16) email_record.code = code email_record.email = email email_record.send_type = send_type email_record.save() # 定义邮件内容: email_title = "" email_body = "" if send_type == 'register': email_title = "Hood site 注册激活链接" email_body = "请点击下面的链接激活你的账号: http://127.0.0.1:8000/active/{0}".format( code) # 使用Django内置函数完成邮件发送。四个参数:主题,邮件内容,从哪里发,接受者list send_status = send_mail(email_title, email_body, EMAIL_FROM, [email]) # 如果发送成功 if send_status: pass elif send_type == 'forget': email_title = "Hood site 重置密码链接" email_body = "请点击下面的链接重置你的密码: http://127.0.0.1:8000/reset/{0}".format( code) send_status = send_mail(email_title, email_body, EMAIL_FROM, [email]) if send_status: pass elif send_type == 'update_email': email_title = "Hood site 邮箱修改验证码" email_body = "邮箱验证码为: {0}".format(code) send_status = send_mail(email_title, email_body, EMAIL_FROM, [email]) if send_status: pass
def send_email(email, send_type="register"): """ 发送邮箱链接 激活链接原理: 1. Server 随机生成一段随即字符串,并保存到数据库,将其和url地址连接起来 2. 将邮箱验证链接发送到用户的邮箱 3. 当用户点击 url 链接后,进入到 Server 的路由匹配,Server 提取出随机字符串 4. 将邮箱和随机字符串和数据库中的字段进行对比 5. 如果一致,则邮箱激活成功 6. 如果不一致,则邮箱激活失败 :param email: 目标邮箱 :param send_type: `register` 表示是激活链接 `forget` 表示是找回密码链接 :return: """ # 1. 先实例化一个EmailVerifyCode对象,将其保存到数据库,供后面验证一致性 email_record = EmailVerifyCode() email_record.email = email if send_type in ["register", "forget"]: email_record.send_type = send_type email_record.code = generate_random_str() email_record.save() # 2. 定义邮件 e 的主题和消息 e_subject = "" e_message = "" url = reverse('user:active', args=(email_record.code, )) if send_type == "register": e_subject = "慕学在线网注册激活链接" e_message = "请点击下面的链接激活您的账户 127.0.0.1:8000{}".format(url) elif send_type == "forget": # TODO 找回密码链接 e_subject = "慕学在线网找回密码链接" e_message = "请点击下面的链接来跳转到修改密码页面 127.0.0.1:8000{}/".format(url) else: pass # 3. 使用django提供的发送邮件函数 django.core.mail.send_mail # 需要提前配置好 邮件发送者的信息 send_status = mail.send_mail(subject=e_subject, message=e_message, from_email=EMAIL_FROM, recipient_list=[ email, ]) return send_status
def send_email_code(email, send_type): """ 第一步创建邮箱验证码,保存数据库,用来以后作对比 """ a = EmailVerifyCode() a.email = email a.send_type = send_type code = get_randon_code(CODE_SIZE) a.code = code a.save() if send_type == 1: send_title = '欢迎注册谷粒教育网站:' send_body = '请点击一下连接进行激活您的账号:\nhttp://127.0.0.1:8000/users/user_active/' + code result = send_mail(send_title, send_body, EMAIL_FROM, [email]) elif send_type == 2: # 发送修改密码的验证邮件 send_title = '谷粒教育网站忘记密码:' send_body = '请点击一下连接修改您的账号密码:\nhttp://127.0.0.1:8000/users/user_reset/' + code result = send_mail(send_title, send_body, EMAIL_FROM, [email]) else: # 发送修改邮箱的验证邮件 pass
def send_email(email, type='register'): """ 发送邮件的方法 register: 注册账号 forget: 找回密码 change: 修改邮箱 """ if type == 'register': subject = '彬彬商场注册激活链接' code_str = generate_random_str(16) message = '请点击下面的链接激活您的账号: http://127.0.0.1:8000/active/{0}'.format( code_str) elif type == 'forget': subject = '彬彬商场忘记密码连接' code_str = generate_random_str(8) timestamp = int(time.time()) md5 = hashlib.md5() md5_str = md5.update( (code_str + email + str(timestamp)).encode('utf8')) hash_str = md5.hexdigest() message = '请点击下面的链接修改你的密码: http://127.0.0.1:8000/reset?timestamp={0}&hash={1}&email={2}'.format( timestamp, hash_str, email) elif type == 'change': subject = '彬彬商场修改邮箱连接' code_str = generate_random_str(6) message = '你的邮箱验证码为: {0}'.format(code_str) else: return False status = send_mail(subject, message, EMAIL_FROM, [email]) if status: # 发送成功 email_code = EmailVerifyCode() email_code.email = email email_code.code = code_str email_code.type = type email_code.save() return True else: return False
def send_mail_code(email, send_type): ## 1.创建邮箱验证码对象,保存数据,用以后作对比 emailVerifyCode = EmailVerifyCode() emailVerifyCode.email = email emailVerifyCode.send_type = send_type # 获取验证码 code = get_random_code(8) emailVerifyCode.code = code emailVerifyCode.save() ## 2.settings.py配置信息 # EMAIL_HOST = 'smtp.163.com' # smpt服务地址 # EMAIL_PORT = 25 # 端口号 # EMAIL_HOST_USER = '******' # 发送邮件的邮箱地址即发件人 # EMAIL_HOST_PASSWORD = '******' # 发送邮件的邮箱[即发件人]中设置的客户端授权密码 # EMAIL_FROM = '谷粒教育<*****@*****.**>' # 收件人看到的发件人 ## 3.发送邮件的具体内容信息 # 激活用户 if send_type == 1: send_title = "欢迎注册谷粒教育网站:" send_body = "请点击以下链接,进行激活账号: \n http://127.0.0.1:8000/users/user_active/" + code # 发送邮件 send_mail(send_title, send_body, settings.EMAIL_FROM, [email]) # 重置密码 if send_type == 2: send_title = "谷粒教育重置密码系统:" send_body = "请点击以下链接,进行重置密码: \n http://127.0.0.1:8000/users/user_reset/" + code # 发送邮件 send_mail(send_title, send_body, settings.EMAIL_FROM, [email]) # 修改邮箱-获取验证码 if send_type == 3: send_title = "谷粒教育重置邮箱验证码:" send_body = "你的邮箱验证码是: " + code # 发送邮件 send_mail(send_title, send_body, settings.EMAIL_FROM, [email])
def send_email_code(email, send_type): # 第一步:创建邮箱验证码对象,保存至数据库,用来以后做对比 code = get_uuid() a = EmailVerifyCode() a.email = email a.send_type = send_type a.code = code a.save() # 第二步:发送邮件 send_title = '' send_body = '' if send_type == 1: send_title = '欢迎注册谷粒教育网站' send_body = '请点击以下链接进行激活您的账号:\n http://127.0.0.1:8000/users/user_active/' + str( code) try: send_mail(send_title, send_body, EMAIL_FROM, [email]) return True except: return False if send_type == 2: send_title = '谷粒教育重置密码' send_body = '请点击以下链接进行重置您的账号密码:\n http://127.0.0.1:8000/users/user_reset/' + str( code) try: send_mail(send_title, send_body, EMAIL_FROM, [email]) return True except: return False if send_type == 3: send_title = '重新绑定邮箱' send_body = '验证码:\n' + str(code) try: send_mail(send_title, send_body, EMAIL_FROM, [email]) return True except: return False
def send_email(email, send_type='register'): ###########################生成随机验证码存入数据库中########################### #继承模型 email_record = EmailVerifyCode() if send_type == 'update': code = random_str(4) #如果是更新邮箱则发送4位验证码 else: code = random_str(16) #将随机生成code存入数据库中 email_record.code = code #将管理的邮箱传入数据库中 email_record.email = email #定义发送类型 email_record.send_type = send_type email_record.save() ###########################发送激活邮件########################### email_title = '' email_body = '' receive_email = email if send_type == 'register': email_title = '慕学在线激活连接' email_body = '请点击下方连接,激活注册:' + 'http://127.0.0.1:8000/active/{0}'.format( code) if send_type == 'find_password': email_title = '慕学在线密码找回连接' email_body = '请点击下方连接,进行密码找回:' + 'http://127.0.0.1:8000/password_reset/{0}'.format( code) if send_type == 'update': email_title = '慕学在线更新邮箱的验证码' email_body = '更新邮箱的验证码是:' + code send_status = send_mail(email_title, email_body, EMAIL_FROM, [receive_email])
def send_email_code(email,send_type): '''这是发送邮箱验证码的函数''' #第一步:创建验证码,存到数据库,后面用来比对 code=get_random_code(8) evc=EmailVerifyCode() evc.email=email evc.send_type=send_type evc.code=code evc.save() #第二步:发邮件 send_title='' send_body='' if send_type==1: send_title='欢迎注册谷粒教育' send_body='请点击以下链接激活您的账号:\nhttp://127.0.0.1:8000/users/user_active/'+code send_mail(send_title,send_body,EMAIL_FROM,[email]) if send_type==2: send_title='谷粒教育重置密码' send_body='请点击以下链接重置您的密码:\nhttp://127.0.0.1:8000/users/user_reset/'+code send_mail(send_title,send_body,EMAIL_FROM,[email]) if send_type==3: send_title='谷粒教育修改邮箱验证码' send_body='下面是您的验证码(5分钟内有效):\n'+code send_mail(send_title,send_body,EMAIL_FROM,[email])
def send_mail_code(email, type): a = EmailVerifyCode() a.email = email a.send_type = type a.code = get_random_code(6)