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

  • 相关阅读:
    CI登录验证
    兼容所有浏览器的JS动态显示当前日期时间
    Active Record 数据库模式-增删改查操作
    数据库链接的增操作举例
    CI框架获取post和get参数 CodeIgniter
    PHP日期操作类代码-农历-阳历转换、闰年、计算天数等
    根据两点间的经纬度计算距离
    Session 类
    mysql 查询优化规则
    php获取文件夹下面的文件列表和文件夹列表
  • 原文地址:https://www.cnblogs.com/Erick-L/p/6882698.html
Copyright © 2011-2022 走看看