新聞中心
郵件擴(kuò)展

成都創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的沙坡頭網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
在開發(fā)過程中,很多應(yīng)用程序都需要通過郵件提醒用戶,F(xiàn)lask的擴(kuò)展包Flask-Mail通過包裝了Python內(nèi)置的smtplib包,可以用在Flask程序中發(fā)送郵件。
Flask-Mail連接到簡單郵件協(xié)議(Simple Mail Transfer Protocol,SMTP)服務(wù)器,并把郵件交給服務(wù)器發(fā)送。
設(shè)置郵箱授權(quán)碼
如下示例,通過開啟 QQ 郵箱驗(yàn)證 SMTP 服務(wù)設(shè)置,發(fā)送郵件:
#coding:utf-8 from flask import Flask,render_template from flask_mail import Mail, Message from threading import Thread app = Flask(__name__) # 配置郵件:服務(wù)器/端口/安全套接字層/郵箱名/授權(quán)碼 app.config['MAIL_SERVER'] = "smtp.126.com" app.config['MAIL_PORT'] = 465 app.config['MAIL_USE_SSL'] = True app.config['MAIL_USERNAME'] = "[email protected]" app.config['MAIL_PASSWORD'] = "19940414" app.config['MAIL_DEFAULT_SENDER'] = 'FlaskAdmin' mail = Mail(app) def async_send_email(app, msg): with app.app_context(): try: mail.send(msg) except Exception as e: print e def send_email_thread(subject, to, content): msg = Message(subject=subject, recipients=[to], body=content) thread = Thread(target=async_send_email,args=(app, msg)) thread.start() return thread @app.route('/') def index(): return '發(fā)送郵件' % url_for('send_email') @app.route('/send_email') def send_email(): send_email_thread('我是郵件主題', to='[email protected]', content='我是郵件內(nèi)容哈哈') return '發(fā)送中...' if __name__ == '__main__': app.run()
眾多python培訓(xùn)視頻,盡在python學(xué)習(xí)網(wǎng),歡迎在線學(xué)習(xí)!
本文轉(zhuǎn)自:https://blog.csdn.net/Enjolras_fuu/article/details/82793428
本文名稱:創(chuàng)新互聯(lián)Python教程:Flask:郵件擴(kuò)展
新聞來源:http://m.fisionsoft.com.cn/article/coschji.html


咨詢
建站咨詢
