原来发送邮件也没有想象中的那么难,直接贴代码:
# coding:utf8 import os from flask import Flask from flask_mail import Mail, Message # pip install flask-mail app = Flask(__name__) # 配置163邮箱服务器,如果是QQ邮箱,则MAIL_SERVER='smtp.qq.com' 但是QQ邮箱需要授权码登录第三方客户端,163比较方便 app.config.update( DEBUG=True, MAIL_SERVER='smtp.163.com', MAIL_PORT=25, MAIL_USE_TLS=True, MAIL_USE_SSL=False, MAIL_USERNAME=os.environ.get('MAIL_USERNAME'), #邮件的用户名和密码可以在系统的环境变量中进行添加 MAIL_PASSWORD=os.environ.get('MAIL_PASSWORD'), MAIL_DEBUG=True ) mail = Mail(app) @app.route('/') def index(): # sender:发送方,recipient邮件接收方列表 msg = Message("Hi!This is a test", sender=os.environ.get('MAIL_USERNAME'), recipients=['18763823073@163.com']) # body:邮件正文 msg.body = 'This is the email content' # msg.attach("文件名","类型",读取文件) 邮件附件添加 with app.open_resource("G:\\1.jpg") as fp: msg.attach("image.jpg", "image/jpg", fp.read()) mail.send(msg) print "Mail Sent" return "Sent" if __name__ == '__main__': app.run(debug=True)