flask框架email认证模块

Email认证

认证过程

1需要配置如下的邮件服务器参数

app.config['MAIL_SERVER']=MAILSERVER
app.config['MAIL_PORT']=MAILPORT
app.config['MAIL_USE_SSL']=MAILUSESSL
app.config['MAIL_USERNAME']=MAILUSERNAME
app.config['MAIL_PASSWORD']=MAILPASSWORD
app.config['FLASKY_MAIL_SUBJECT_PREFIX']=FLASKY_MAIL_SUBJECT_PREFIX
app.config['FLASKY_MAIL_SENDER']=FLASKY_MAIL_SENDER

2.User类型需要添加如下函数

def generate_activate_token(self, expires_in=3600):
        s = Serializer(current_app.config['SECRET_KEY'], expires_in=expires_in)
        return s.dumps({'id': self.id})

    @staticmethod
    def check_activate_token(self,token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except:
            return False
        user = User.query.get(data['id'])
        if not user:
            # 用户已被删除
            return False
        # 没有激活时才需要激活
        if not user.confirmed:
            user.confirmed = True
            db.session.add(self)
            db.session.commit()
        return True

3.下面是自己写的一个邮件发送函数

def send_mail(receivers,subject,annex,**kwargs):
    message=Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX']+subject,sender=MAILUSERNAME,recipients=[receivers])
    with app.app_context():
        message.body=render_template('user.txt',**kwargs)
        mcc_print('hello_mcc')
#下面是我临时测试用的       #message.html=render_template('activate.html',token='user')
        mcc_print('hello')
    if annex == None:
        with app.app_context():
            mail.send(message)
    else:
        with app.app_context():
            with app.open_resource(annex) as f:
                array=re.split(r'[.,/]',annex)
                type=array[-1]
                mcc_print(type)
                if type=='jpg' or type=='bmp' or type=='gif' or type=='bmp' or type=='png':
                    message.attach(annex,'image/'+type,f.read())
                    mcc_print("文件格式:jpg")
                elif type=='mp4' or type=='mkv' or type=='avi':
                    message.attach(annex,'vedio'+type,f.read())
                    mcc_print("文件格式:mp4")
                elif type=='doc' or type=='docx' or type=='txt' or type=='xls' or type=='pptx' or type=='ppt' :
                    message.attach(annex,'doc/'+type,f.read())
                    mcc_print("文件格式:txt")
                else:
                    mcc_print("文件格式不支持")
                mail.send(message)

4.最后是activate路由

@app.route('/activate/<token>')
def activate(token):
    if User.check_activate_token(token):
        flash('激活成功')
        return redirect(url_for('login'))
    else:
        flash('激活失败')
        return redirect(url_for('index'))

4.前端activate模板

<h1>Hello {{ username }}</h1>
<p>激活请点击右边链接,<a href="http://127.0.0.1/activate/{{token}}">激活</a></p>

这里也是遇到了坑的,参考的一篇博文上使用的url_for(‘acivate’,token=token,_extern=True),在自己的电脑上测试时没有通过的,因此自己重新写了一下。

总结:

在配置邮件服务器时遇到了特别多的坑,首先是用的gmail,再到qq邮箱,看到有的博文上说qq邮箱只能使用ssl,外加465端口,觉得有点奇葩,和官网上的说明差了很多

mcc

感觉无语,另一点,也于是我比较倒霉吧,在教学楼里调程序,始终出现connect close 提示,但回到宿舍再次调试时立马就好了,可能是编辑器不能及时响应吧
这几个月的flask框架学习,感觉还是很多的,我觉得flask应该从蓝图学起,不然自己对整个框架只是冰山一角,全部将代码堆在一个文件里,这一点我的感受特别深,改起来特别麻烦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值