diff --git a/daily_report/test_report.py b/daily_report/test_report.py index 87785ba..4819bc7 100644 --- a/daily_report/test_report.py +++ b/daily_report/test_report.py @@ -1,8 +1,13 @@ # -*- coding: utf-8 -*- from flask import Flask, render_template, jsonify,url_for -from flask_mail import Mail, Message +# from flask_mail import Mail, Message import os 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') @@ -15,36 +20,62 @@ app.config['MAIL_USE_SSL'] = True app.config['MAIL_USE_TLS'] = False ## 默认就是 false, 加上警示自己 app.config['MAIL_USERNAME'] = sender app.config['MAIL_PASSWORD'] = 'bX8cfBAyj9MBqH22' -mail = Mail(app) +# mail = Mail(app) recipients = ['pengtao@kingsome.cn'] -def send_async_email(app, msg): - with app.app_context(): - mail.send(msg) +def add_img(file, imgid, msg): + fp = open(file, 'rb') + img = MIMEImage(fp.read()) + fp.close() + img.add_header("Content-ID", f'<{imgid}>') + html = f"
" + content = MIMEText(html, 'html', 'utf-8') + msg.attach(content) + msg.attach(img) + return msg @app.route('/') def show_index(): title = "test报表" - # day = request.args.get('day') - # project = request.args.get('project') or 'mini_games' + # msg = Message(title, sender=sender, recipients=recipients) + 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 = os.path.join(app.config['UPLOAD_FOLDER'], 'img2019-10-31.png') - full_filename = url_for("static",filename= 'images/img2019-10-31.png') - print(full_filename) - msg.html = render_template('index.html', user_image=full_filename) + # full_filename = url_for("static",filename= 'images/img2019-10-31.png') + # filename = "/root/miles/test/static/images/img2019-10-31.png" + # import base64 + # img_file = open(filename, 'rb') + # base64_data = base64.b64encode(img_file.read()) + # html = f"""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("邮件发送成功") if __name__ == "__main__": - # main() + # show_index() # main() # app.run(host='0.0.0.0', port=9700, debug=False)