• 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

  • 相关阅读:
    weblogic 未授权命令执行漏洞(CVE-2020-14882,CVE-2020-14883)复现
    sqlmap之--os-shell命令执行原理
    HTTP协议层面绕过WAF
    windows下过安全狗
    [SUCTF 2019]Pythonginx
    warmup(HCTF 2018)
    phpmyadmin 4.8.1 远程文件包含漏洞(CVE-2018-12613)
    phpMyAdmin写入WebShell(二)
    phpMyAdmin写入WebShell(一)
    [SUCTF 2019]EasySQL
  • 原文地址:https://www.cnblogs.com/Erick-L/p/6882698.html
Copyright © 2020-2023  润新知