Python Flask異步發(fā)送郵件實現(xiàn)方法解析
更新時間:2020年08月01日 14:39:51 作者:viewts
這篇文章主要介紹了Python Flask異步發(fā)送郵件實現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
第一步,修改工廠函數(shù),配置郵件參數(shù)
from flask import Flask from config import Config from flask_sqlalchemy import SQLAlchemy from flask_mail import Mail db = SQLAlchemy() mail = Mail() def create_app(): app = Flask(__name__) app.config.from_object(Config) db.init_app(app) mail.init_app(app) from .controller import controller app.register_blueprint(controller) return app
第二步,新建一個線程來發(fā)送郵件
from flask import current_app, render_template from flask_mail import Message from threading import Thread from main import mail def send_async_email(app, msg): with app.app_context(): mail.send(msg) def send_email(to, subject, template = 'index', **kwargs): app = current_app._get_current_object() msg = Message(subject, sender = app.config['MAIL_USERNAME'], recipients = [to]) msg.html = render_template('{}.html'.format(template), **kwargs) thr = Thread(target = send_async_email, args = [app, msg]) thr.start() return thr
從current_app的_get_current_object()方法拿到應用程序上下文。特此記錄一下
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
python?中defaultdict()對字典進行初始化的用法介紹
這篇文章主要介紹了python?中defaultdict()對字典進行初始化,一般情況下,在使用字典時,先定義一個空字典(如dict_a?=?{}),然后往字典中添加元素只需要?dict_a[key]?=?value即可,本文通過實例代碼介紹具體用法,需要的朋友可以參考下2022-07-07python實現(xiàn)動態(tài)數(shù)組的示例代碼
這篇文章主要介紹了python實現(xiàn)動態(tài)數(shù)組的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07python 獲取文件下所有文件或目錄os.walk()的實例
下面小編就為大家分享一篇python 獲取文件下所有文件或目錄os.walk()的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04