本篇博客記錄flask-mail的使用
from flask import Flask
from flask_mail import Mail, Message
app = Flask(__name__)
app.config['MAIL_SERVER'] = 'smtp.163.com'
app.config['MAIL_PORT'] = '465'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = 'xxxxxxxxx' #郵箱服務器提供的授權碼
mail = Mail(app)
@app.route('/sendmail')
def send_mail():
msg = Message(subject="郵件測試", sender="[email protected]", recipients=['[email protected]'])
msg.body = '驗證碼:111111'
mail.send(msg)
return "郵件發送成功"
if __name__ == "__main__":
app.run('0.0.0.0', debug=True)
瀏覽器訪問:http://127.0.0.1:5000/sendmail
顯示:郵件發送成功