美文网首页
python flask 中发送邮件用线程进行发送

python flask 中发送邮件用线程进行发送

作者: bridge92 | 来源:发表于2017-12-04 11:27 被阅读0次

    from  flask  import  Flask

    from flask_mail import Mail, Message

    from threading import Thread

    app = Flask(__name__)

    app.config["MAIL_SERVER"] = " s m t p .163.com " 代理邮箱服务器的名字

    app.config['MAIL_PORT'] = 465  #端口

    app.config['MAIL_USE _  SSL'] = True

    app.config['MAIL_USERNAME'] = "good study@163.com" #发送者的邮箱

    app.config['MAIL_PASSWORD'] = "  "#授权码

    app.config['MAIL_DEFAULT_SENDER'] =  'Flask Admin '#默认的发送邮件的地址

    mail = Mail(app)

    def a s y c_send_mail(message):

    # 新线程里面是没有开启应用上下文的

    # 下面这句代码是手动开启应用上下文

    with app.app_context():

    mail.send(message)

    @app.route('/')

    def index():

    return'发送邮件'

    @app.route('/send_mail')

    def send_mail():

    message = Message('邮件主题', recipients=['15010860193@163.com',])#发送给谁可以给多个邮箱进行发送 message.html="

    哈哈

    "

    thread = Thread(target=asyc_send_mail, args=(message,))

    thread.start()

    return '发送中'

    if __name__ == '__main__':

    app.run(debug=True ,port=5001)

    相关文章

      网友评论

          本文标题:python flask 中发送邮件用线程进行发送

          本文链接:https://www.haomeiwen.com/subject/jxgtixtx.html