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

  • 相关阅读:
    loj#6433. 「PKUSC2018」最大前缀和(状压dp)
    PKUWC2019游记
    10. Regular Expression Matching
    9. Palindrome Number
    8. String to Integer (atoi)
    7. Reverse Integer
    6. ZigZag Conversion
    5. Longest Palindromic Substring
    4. Median of Two Sorted Arrays
    3. Longest Substring Without Repeating Characters
  • 原文地址:https://www.cnblogs.com/Erick-L/p/6882698.html
Copyright © 2011-2022 走看看