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()