82 lines
2.6 KiB
Python
82 lines
2.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
from flask import Flask, render_template, jsonify,url_for
|
|
# 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')
|
|
|
|
app = Flask(__name__)
|
|
app.config['UPLOAD_FOLDER'] = PEOPLE_FOLDER
|
|
sender = "ops@kingsome.cn"
|
|
app.config['MAIL_SERVER'] = 'smtp.exmail.qq.com'
|
|
app.config['MAIL_PORT'] = '465'
|
|
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)
|
|
recipients = ['pengtao@kingsome.cn']
|
|
|
|
|
|
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"<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('/')
|
|
def show_index():
|
|
title = "test报表"
|
|
# 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'
|
|
|
|
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")
|
|
|
|
# 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"""<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
|
|
|
|
|
|
return jsonify("邮件发送成功")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# show_index() # main() #
|
|
app.run(host='0.0.0.0', port=9700, debug=False)
|