zoukankan      html  css  js  c++  java
  • Flask 学习 五 电子邮件

    pip install mail

     from flask_mail import Mail
    # 邮件配置
    app.config['MAIL_SERVER']='smtp.qq.com'
    app.config['MAIL_PORT']=465
    app.config['MAIL_USE_TLS']=True
    app.config['MAIL_USERNAME']=os.environ.get('MAIL_USERNAME')
    app.config['MAIL_PASSWORD']=os.environ.get('MAIL_PASSWORD')
    mail =Mail(app)
    
    # shell中发送电子邮件
    Python hello.py shell
    from flask.ext.mail import Message
    from hello import mail
    msg = Message('test subject', sender='you@example.com',recipients=['you@example.com'])
    msg.body = 'text body'
    msg.html = '<b>HTML</b> body'
    with app.app_context():
        mail.send(msg)

    程序集成发邮件功能

    from threading import Thread
    from flask_mail import Mail,Message
    # 邮件配置
    app.config['MAIL_SERVER']='smtp.qq.com'
    app.config['MAIL_PORT']=465
    app.config['MAIL_USE_TLS']=True
    app.config['MAIL_USERNAME']=os.environ.get('MAIL_USERNAME')
    app.config['MAIL_PASSWORD']=os.environ.get('MAIL_PASSWORD')
    app.config['FLASKY_MAIL_SUBJECT_PREFIX']='[Flasky]'
    app.config['FLASKY_MAIL_SENDER']='Flasky Admin <flasky@example.com>'#发件人
    app.config['FLASKY_ADMIN'] = os.environ.get('FLASKY_ADMIN')  #收件人
    mail =Mail(app)
    def send_async_email(app,msg):
        with app.app_context():
            mail.send(msg)
    
    def send_mail(to,subject,template,**kwargs):
        msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + subject,sender=app.config['FLASKY_MAIL_SENDER'],recipients=[to])
        msg.body=render_template(template + '.txt',**kwargs)
        msg.html = render_template(template + '.html', **kwargs)
        thr = Thread(target=send_async_email,args=[app,msg])
        thr.start()
        return thr

    配置环境变量:set FLASKY_ADMIN=3153324684@qq.com

  • 相关阅读:
    LAMP的搭建
    linux利用命令重置大量密码
    CSS的应用下
    Day13 CSS的与应用
    Day12 CSS简单用法
    Day12 前端html
    Day11 数据库的基本语法(偏重于查询)
    Java-->把txt中的所有字符按照码表值排序
    Java-->将txt文件的所有行反转
    Java-->在txt文件每一行前加行数和冒号
  • 原文地址:https://www.cnblogs.com/Erick-L/p/6882698.html
Copyright © 2011-2022 走看看