首先直接貼上代碼
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
from flask_mail import Mail, Message
app = Flask(__name__)
app.config['MAIL_SERVER'] = 'smtp.qq.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'xxxx@qq.com'
app.config['MAIL_PASSWORD'] = '授權碼'
mail = Mail(app)
msg = Message('test subject', sender='xxxx@qq.com',recipients=['xxxx@xxx.com'])
msg.body = u'我的第一個FLASK郵件測試'
with app.app_context():
mail.send(msg)
1.安裝 Flask-Mail
(venv) $ pip install flask-mail
2.Flask-Mail SMTP服務器的配置
各項配置參照下圖
Paste_Image.png
QQ郵箱授權碼獲取
登錄QQ郵箱,進入設置頁面
Paste_Image.png
切換到賬戶選項
開啟PO3/IMAP服務
Paste_Image.png
通過發送短信認證就可以拿到授權碼啦 - -
運行腳本
把授權碼填上,運行腳本就可以把郵件發送出去啦!!!!
就是這么簡單!!!!!!!!!!!!!
運行效果
Paste_Image.png