{{ v.name }}
{{ v.cls }}类
{{ v.price }} ¥{{ v.price }}
项目中不可避免需要使用邮箱认证,如果使用flask则可以利用flask-mail来实现。
flask-mail扩展提供了一个简单的接口,可以在flask应用中设置smtp使得可以在视图以及脚本中发送邮件信息。
这里我利用的qq邮箱的smtp服务,所以首先需要先开启该服务并获得授权码。在qq邮箱“邮箱设置—账户”里找到下图位置,开启smtp服务。之后手机验证什么的依自己帐号设置,验证成功后会获得一个授权码,这个需要保存后续发送邮箱时密码就填这个授权码。
pipinstallflask-mail配置flask-mail配置项默认值功能mail_serverlocalhost邮箱服务器mail_port25端口mail_use_tlsfalse是否使用tlsmail_use_sslfalse是否使用sslmail_debugapp.debug是否为debug模式,打印调试消息mail_suppress_sendapp.testing设置是否真的发送邮件,true不发送mail_usernamenone用户名,填邮箱mail_passwordnone密码,填授权码mail_default_sendernone默认发送者,填邮箱mail_max_emailsnone一次连接中的发送邮件的上限mail_ascii_attachmentsfalse如果mail_ascii_attachments设置成true的话,文件名将会转换成ascii的。一般用于添加附件。
邮件是通过一个mail实例进行管理:
fromflaskimportflaskfromflask_mailimportmailapp=flask(__name__)#...app.config['mail_server']='smtp.qq.com'app.config['mail_port']=465app.config['mail_use_ssl']=trueapp.config['mail_use_tls']=falseapp.config['mail_username']='xxx@qq.com'app.config['mail_password']='填授权码'#...mail=mail(app)
在这个例子中所有的邮件将会使用传入到mail实例中的应用程序的配置项进行发送。
或者你也可以在应用程序配置的时候设置你的mail实例,通过使用init_app方法:
mail=mail()app=flask(__name__)#...app.config['mail_server']='smtp.qq.com'app.config['mail_port']=465app.config['mail_use_ssl']=trueapp.config['mail_use_tls']=falseapp.config['mail_username']='xxx@qq.com'app.config['mail_password']='填授权码'#...mail.init_app(app)
在这个例子中邮件将会使用flask的current_app中的配置项进行发送。如果你有多个具有不用配置项的多个应用运行在同一程序的时候,这种设置方式是十分有用的,
首先要创建发送邮件的内容message实例:
fromflask_mailimportmessagemsg=message(subject="helloworld!",sender="from@qq.com",recipients=["to@example.com"])
其中,subject为邮件标题。sender为发送方,如果你设置了“mail_default_sender”,就不必再次填写发件人,默认情况下将会使用配置项的发件人。recipients为接收方,可以设置一个或者多个收件人,也可以后续再添加。
msg.recipients=["xxx@qq.com"]msg.add_recipient("xxxx@qq.com")
如果sender是一个二元组,它将会被分成姓名和邮件地址:
msg=message("hello",sender=("me","me@example.com"))
邮件内容可以包含主体以及/或者html:
msg.body="testing"msg.html="testing"
最后,发送邮件的时候请使用flask应用设置的mail实例:
mail.send(msg)大量邮件
通常在一个web应用中每一个请求会同时发送一封或者两封邮件。在某些特定的场景下,有可能会发送数十或者数百封邮件,不过这种发送工作会给交离线任务或者脚本执行。
在这种情况下发送邮件的代码会有些不同:
withmail.connect()asconn:foruserinusers:message='...'subject="hello,%s"%user.namemsg=message(recipients=[user.email],body=message,subject=subject)conn.send(msg)
与电子邮件服务器的连接会一直保持活动状态直到所有的邮件都已经发送完成后才会关闭(断开)。
有些邮件服务器会限制一次连接中的发送邮件的上限。你可以设置重连前的发送邮件的最大数,通过配置mail_max_emails。
在邮件中添加附件同样非常简单:
withapp.open_resource("image.png")asfp:msg.attach("image.png","image/png",fp.read())
如果mail_ascii_attachments设置成true的话,文件名将会转换成ascii的。当文件名是以utf-8编码的时候,使用邮件转发的时候会修改邮件内容并且混淆content-disposition描述,这个时候mail_ascii_attachments配置项是十分有用的。转换成ascii的基本方式就是对non-ascii字符的去除。任何一个unicode字符能够被nfkd分解成一个或者多个ascii字符。
假设你已经开启了qq邮箱的stmp服务以及配置好flask和flask-mail环境。
#-*-coding:utf-8-*-fromflaskimportflask,requestfromflask_scriptimportmanager,shellfromflask_mailimportmail,messagefromthreadingimportthreadapp=flask(__name__)app.config['mail_debug']=true#开启debug,便于调试看信息app.config['mail_suppress_send']=false#发送邮件,为true则不发送app.config['mail_server']='smtp.qq.com'#邮箱服务器app.config['mail_port']=465#端口app.config['mail_use_ssl']=true#重要,qq邮箱需要使用sslapp.config['mail_use_tls']=false#不需要使用tlsapp.config['mail_username']='xxx@qq.com'#填邮箱app.config['mail_password']='xxxxxx'#填授权码app.config['mail_default_sender']='xxx@qq.com'#填邮箱,默认发送者manager=manager(app)mail=mail(app)#异步发送邮件defsend_async_email(app,msg):withapp.app_context():mail.send(msg)@app.route('/')defindex():msg=message(subject='helloworld',sender="xxx@qq.com",#需要使用默认发送者则不用填recipients=['x1@qq.com','x2@qq.com'])#邮件内容会以文本和html两种格式呈现,而你能看到哪种格式取决于你的邮件客户端。msg.body='sendedbyflask-email'msg.html='测试flask发送邮件'thread=thread(target=send_async_email,args=[app,msg])thread.start()return'邮件发送成功
'if__name__=='__main__':manager.run()
tip:具体工程中,配置可以写在单独一个文件如”.env”,然后利用python-envcfg来读取配置,如:app.config.from_object(‘envcfg.raw’)