1. 程式人生 > >Python Flask,傳送郵件,Flask-Mail,郵件擴充套件

Python Flask,傳送郵件,Flask-Mail,郵件擴充套件

安裝 Flask-Mail : pip install Flask-Mail  

demo.py(傳送郵件):

from flask import Flask
from flask_mail import Mail, Message  # 匯入

app = Flask(__name__)
# 配置郵件:伺服器/埠/傳輸層安全協議/郵箱名/密碼(授權碼)
app.config.update(
    DEBUG = True,
    MAIL_SERVER='smtp.qq.com',
    MAIL_PROT=465,
    MAIL_USE_TLS = True,  # 加密協議
    MAIL_USERNAME = '傳送者@qq.com',
    MAIL_PASSWORD = 'goyubxohbtzfbidd',  # 授權碼
)

# 例項化mail物件
mail = Mail(app)

@app.route('/')
def index():
    # sender:傳送方。 recipients:接收方列表
    msg = Message("主題", sender='傳送者@qq.com', recipients=['接收者
[email protected]
','接收者[email protected]']) # 郵件內容 msg.body = "郵件主體內容" # 傳送郵件 mail.send(msg) return "Sent Succeed" if __name__ == "__main__": app.run()