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

    1.安装flask_mail

    sudo pip install flask_mail

    2.开启邮箱的SMTP服务

    3.测试

    from flask import Flask
    from flask_mail import Mail, Message
    
    import os
    
    app = Flask(__name__)
    app.config.update(
        DEBUG = True,
        MAIL_SERVER = 'smtp.qq.com',
        MAIL_PORT = 25,
        MAIL_USE_TLS = True,
        MAIL_USE_SSL = False,
        MAIL_USERNAME = 'email_server@example.com',  #配置你自己的邮箱服务器
        MAIL_PASSWORD = '*****************',         #配置邮箱服务器的授权码
        MAIL_DEBUG = True)
    
    mail = Mail(app)
    
    @app.route('/')
    def index():
        msg=Message("Hi! This is a test!",sender='from@example.com',recipients=['to@example.com'])
        msg.body = "This is a first email"
        
        with app.open_resource("email_text.txt") as fp:
    msg.attach("email_text.txt","email_text/txt",fp.read())
    
        mail.send(msg)
        print "Mail sent"
        return "Sent"
    
    if __name__ == "__main__":
        app.run()
  • 相关阅读:
    python基础(1)#1,2,3,4可组成多少不重复的三位数
    HTML/CSS 学习笔记
    (转) 杨元:CSS浮动(float,clear)通俗讲解
    前端:HTML
    Servlet
    Maven 安装
    单例模式
    项目随笔
    树状结构--迭代
    DB的封装
  • 原文地址:https://www.cnblogs.com/xinyuefeiyang/p/7237121.html
Copyright © 2011-2022 走看看