flask_mail配置qq邮箱
程序员文章站
2022-07-08 14:45:01
...
flask_mail配置qq邮箱
1:安装flask_mail
在命令行执行pip install flask_mail
2:配置授权码
点击这里生成授权码,后面会用到
3:全部代码
# -*- coding: utf-8 -*-
from flask import Flask, jsonify, request, render_template
from flask_mail import Message
from flask_mail import Mail
start_time = time.time()
app = Flask(__name__)
app.config['MAIL_SERVER'] = 'smtp.qq.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True # 这里要使用ssl
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_DEBUG'] = True # 开启debug 查看报错信息
app.config['MAIL_USERNAME'] = '***@qq.com'
app.config['MAIL_PASSWORD'] = '****' # 授权码不能用空格
app.config['MAIL_DEFAULT_SENDER'] = '****@qq.com' # 默认的邮件发送者
mail = Mail(app)
@app.route("/")
def index():
return render_template("index.html")
@app.route('/send_mail', methods=["GET", "POST"])
def send_mail():
msg = Message("Hello",
recipients=["aaa@qq.com"])
msg.body = "testing"
msg.html = "<b>testing</b>"
mail.send(msg)
return "send_success!"
if __name__ == '__main__':
send_mail()
app.run(debug=True)
4:请求对应的路劲 收到了对应的邮件