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

  • 相关阅读:
    [LeetCode]4Sum
    [LeetCode]3Sum
    [LeetCode]Two Sum
    [LeetCode]Maximal Rectangle
    [LeetCode]Largest Rectangle in Histogram
    [LeetCode]Sudoku Solver
    [LeetCode]Group Anagrams
    jQuery验证控件jquery.validate.js使用说明+中文API
    js操作cookie,实现登录密码保存
    Java中getResourceAsStream的用法
  • 原文地址:https://www.cnblogs.com/Erick-L/p/6882698.html
Copyright © 2011-2022 走看看