flask_mail配置qq邮箱

flask_mail配置qq邮箱

1:安装flask_mail
在命令行执行pip install flask_mail

2:配置授权码
点击这里生成授权码,后面会用到
flask_mail配置qq邮箱_第1张图片

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=["[email protected]"])
    msg.body = "testing"
    msg.html = "testing"
    mail.send(msg)
    return "send_success!"


if __name__ == '__main__':
    send_mail()
    app.run(debug=True)

4:请求对应的路劲 收到了对应的邮件
flask_mail配置qq邮箱_第2张图片

你可能感兴趣的:(Python学习)