zoukankan      html  css  js  c++  java
  • flask邮件发送

    安装flask-mail

    pip3 install flask-mail

    开启smtp服务

    发送邮件

    from flask import Flask, render_template
    from flask.ext.mail import Mail, Message
    import os
    
    app = Flask(__name__)
    app.config.update(
        DEBUG = True,
        MAIL_SERVER='smtp.126.com',
        MAIL_PROT=25,
        MAIL_USE_TLS = True,
        MAIL_USE_SSL = False,
        MAIL_USERNAME = 't2514553187@126.com',
        MAIL_PASSWORD = 'xxxxxxxxxxx',
        MAIL_DEBUG = True
    )
    
    mail = Mail(app)
    
    def send_email(subject, sender, recipients, text_body, html_body):
        msg = Message(subject, sender, recipients)
        msg.body = text_body
        msg.html = html_body
        mail.send(msg)
    
    
    @app.route('/')
    def index():
    # sender 发送方哈,recipients 邮件接收方列表
        msg = Message("Hi!This is a test ",sender='t2514553187@126.com', recipients=['2514553187@qq.com'])
    # msg.body 邮件正文
        msg.body = 'wocaonima'
        msg.html = "<a href='http://www.baidu.com'>This is a first email1111</a>"
    # msg.attach 邮件附件添加
    
        mail.send(msg)
        return "Sent"
    
    if __name__ == "__main__":
        app.run()
    

    发送附件

    from flask import Flask, render_template
    from flask.ext.mail import Mail, Message
    
    app = Flask(__name__)
    app.config.update(
        DEBUG = True,
        MAIL_SERVER='smtp.126.com',
        MAIL_PROT=25,
        MAIL_USE_TLS = True,
        MAIL_USE_SSL = False,
        MAIL_USERNAME = 't2514553187@126.com',
        MAIL_PASSWORD = 'sishen44448888',
        MAIL_DEBUG = True
    )
    
    mail = Mail(app)
    
    def send_email(subject, sender, recipients, text_body, html_body):
        msg = Message(subject, sender, recipients)
        msg.body = text_body
        msg.html = html_body
        mail.send(msg)
    
    
    @app.route('/')
    def index():
    # sender 发送方哈,recipients 邮件接收方列表
        msg = Message("Hi!This is a test ",sender='t2514553187@126.com', recipients=['2514553187@qq.com'])
    # msg.attach 邮件附件添加
    # msg.attach("文件名", "类型", 读取文件)
        with app.open_resource("./stu.png") as fp:
            msg.attach("image.jpg", "image/jpg", fp.read())
    
        mail.send(msg)
        return "Sent"
    
    if __name__ == "__main__":
        app.run()
    

    异步发送邮件

    from flask import Flask, render_template
    from flask.ext.mail import Mail, Message
    from threading import Thread
    
    def async(f):
        def wrapper(*args, **kwargs):
            thr = Thread(target = f, args = args, kwargs = kwargs)
            thr.start()
        return wrapper
    
    app = Flask(__name__)
    app.config.update(
        DEBUG = True,
        MAIL_SERVER='smtp.126.com',
        MAIL_PROT=25,
        MAIL_USE_TLS = True,
        MAIL_USE_SSL = False,
        MAIL_USERNAME = 't2514553187@126.com',
        MAIL_PASSWORD = 'sishen44448888',
        MAIL_DEBUG = True
    )
    import time
    mail = Mail(app)
    
    @async
    def send_async_email(msg):
        time.sleep(10)
        with app.app_context():
            mail.send(msg)
    
    def send_email(subject, sender, recipients, text_body, html_body):
        msg = Message(subject, sender = sender, recipients = recipients)
        msg.body = text_body
        msg.html = html_body
        send_async_email(msg)
    
    @app.route('/')
    def index():
        send_email("This is a test", sender='t2514553187@126.com', recipients=['2514553187@qq.com'],
                   text_body='nihao', html_body="<a href='http://www.baidu.com'>This is a first email</a>")
        return "Sent"
    
    if __name__ == "__main__":
        app.run()
    
  • 相关阅读:
    Docker-常用命令
    5分钟了解折半插入排序
    Spring框架之IOC原理
    使用JS实现简单喷泉效果
    坦克大战系列6-API常用函数说明1
    坦克大战系列6-API常用函数说明2
    为什么要使用-Docker
    SQL语言:存储过程
    使用原生JS重构简单的音乐播放器
    [区间DP]ZOJ3541 The Last Puzzle
  • 原文地址:https://www.cnblogs.com/longyunfeigu/p/9284777.html
Copyright © 2011-2022 走看看