h5制作軟件推薦菏澤資深seo報(bào)價(jià)
一、安裝Flask-Mail擴(kuò)展
pip install Flask-Mail
二、配置Flask-Mail
格式:app.config['參數(shù)']='值'
三、實(shí)現(xiàn)方法
3.1、Mail類
常用類方法
3.2、Message類,它封裝了一封電子郵件。構(gòu)造函數(shù)參數(shù)如下:
flask-mail.Message(subject, recipients, body, html, sender, cc, bcc, reply-to, date, charset, extra_headers, mail_options, rcpt_options)
其它方法:
attach(filename,content_type,data) - 為郵件添加附件。filename:附件名、content_type - MIME類型的文件、data - 原始文件數(shù)據(jù)
add_recipient() - 向郵件添加另一個(gè)收件人
四、舉例說(shuō)明
from flask import Flask
from flask_mail import Mail, Messageapp =Flask(__name__)app.config['MAIL_SERVER']='smtp.gmail.com' #配置郵箱
app.config['MAIL_PORT'] = 456
app.config['MAIL_USERNAME'] = 'tester01@gmail.com'
app.config['MAIL_PASSWORD'] = '*****'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app) #創(chuàng)建Mail類實(shí)例@app.route("/")
def index():msg = Message('Hello', sender = 'tester01@gmail.com', recipients = ['tester02@gmail.com'])msg.body = "Hello World"mail.send(msg)return "Sented"if __name__ == '__main__':app.run(debug = True)