游戏日报 grafana->png sendmail

This commit is contained in:
pengtao 2019-11-01 10:51:04 +08:00
parent 52d25a9d8b
commit cf38ac3335

View File

@ -1,8 +1,13 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from flask import Flask, render_template, jsonify,url_for from flask import Flask, render_template, jsonify,url_for
from flask_mail import Mail, Message # from flask_mail import Mail, Message
import os import os
from threading import Thread from threading import Thread
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header
import smtplib
PEOPLE_FOLDER = os.path.join(os.path.abspath('.'), '/static/images') PEOPLE_FOLDER = os.path.join(os.path.abspath('.'), '/static/images')
@ -15,36 +20,62 @@ app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USE_TLS'] = False ## 默认就是 false, 加上警示自己 app.config['MAIL_USE_TLS'] = False ## 默认就是 false, 加上警示自己
app.config['MAIL_USERNAME'] = sender app.config['MAIL_USERNAME'] = sender
app.config['MAIL_PASSWORD'] = 'bX8cfBAyj9MBqH22' app.config['MAIL_PASSWORD'] = 'bX8cfBAyj9MBqH22'
mail = Mail(app) # mail = Mail(app)
recipients = ['pengtao@kingsome.cn'] recipients = ['pengtao@kingsome.cn']
def send_async_email(app, msg): def add_img(file, imgid, msg):
with app.app_context(): fp = open(file, 'rb')
mail.send(msg) img = MIMEImage(fp.read())
fp.close()
img.add_header("Content-ID", f'<{imgid}>')
html = f"<html><body><br><img src=\"cid:{imgid}\" border=\"1\"</br></body></html>"
content = MIMEText(html, 'html', 'utf-8')
msg.attach(content)
msg.attach(img)
return msg
@app.route('/') @app.route('/')
def show_index(): def show_index():
title = "test报表" title = "test报表"
# day = request.args.get('day') # msg = Message(title, sender=sender, recipients=recipients)
# project = request.args.get('project') or 'mini_games' msg = MIMEMultipart('related')
msg['from'] = sender
mail_to = "pengtao@kingsome.cn"
msg['Subject'] = Header(title, 'utf-8')
full_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'img2019-10-31.png')
msg = add_img(full_filename, 'Active', msg)
msgText = MIMEText("邮件正文", 'html', 'utf-8')
msg.attach(msgText)
mail_user = sender
mail_passwd = 'bX8cfBAyj9MBqH22'
msg = Message(title, sender=sender, recipients=recipients) try:
smtObj = smtplib.SMTP_SSL()
smtObj.connect('smtp.exmail.qq.com', 465)
smtObj.login(mail_user, mail_passwd)
COMMSAPACE = ','
smtObj.sendmail(sender, mail_to.split(','), msg.as_string())
except smtplib.SMTPException:
return jsonify("Error")
msg.subject = f"test_游戏日报" # full_filename = url_for("static",filename= 'images/img2019-10-31.png')
#full_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'img2019-10-31.png') # filename = "/root/miles/test/static/images/img2019-10-31.png"
full_filename = url_for("static",filename= 'images/img2019-10-31.png') # import base64
print(full_filename) # img_file = open(filename, 'rb')
msg.html = render_template('index.html', user_image=full_filename) # base64_data = base64.b64encode(img_file.read())
# html = f"""<img src="data: image/png;base64, {base64_data}" alt="image1">"""
#
# # print(full_filename)
# msg.html = render_template('index.html', user_image=full_filename)
# msg.html += html
thread = Thread(target=send_async_email, args=[app, msg])
thread.start()
return jsonify("邮件发送成功") return jsonify("邮件发送成功")
if __name__ == "__main__": if __name__ == "__main__":
# main() # show_index() # main() #
app.run(host='0.0.0.0', port=9700, debug=False) app.run(host='0.0.0.0', port=9700, debug=False)