#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import smtplib
import time
import threading
from email.message import EmailMessage
from tkinter import *
from tkinter import messagebox
to_email = ['[email protected]', '[email protected]',] # 收件
cc_email = ['[email protected]',] # 抄送
m_email = '[email protected]' # 我的邮箱
e_pwd = 'xxxxxx' # 我的密码
s_url = 'smtp.qiye.aliyun.com' # 邮箱服务器
class MyWindow(threading.Thread):
def __init__(self, tk):
super().__init__()
self.tk = tk
self.tk.attributes('-topmost', True)
font = ("微软雅黑", 18)
self.td_label = Label(self.tk, text="今日工作内容:", font=font) # , bd=1, relief="solid"
self.tw_label = Label(self.tk, text="明日工作计划:", font=font)
font2 = ("微软雅黑", 12)
self.td_f = Frame(self.tk)
self.td_sb = Scrollbar(self.td_f)
self.td_text = Text(self.td_f, font=font2, yscrollcommand=self.td_sb.set)
self.tw_f = Frame(self.tk)
self.tw_sb = Scrollbar(self.tw_f)
self.tw_text = Text(self.tw_f, font=font2, yscrollcommand=self.tw_sb.set)
font3 = ("微软雅黑", 16)
self.send_button = Button(self.tk, text="发送", command=self.click_send, font=font3, bd=1, relief="ridge")
# 设置窗口
def init_window(self):
self.tk.title("便捷日报 —— wkf")
self.tk.resizable(False, False)
w_height = 600
w_width = 800
screen_width = self.tk.winfo_screenwidth()
screen_height = self.tk.winfo_screenheight()
x_coordinate = int((screen_width / 2) - (w_width / 2))
y_coordinate = int((screen_height / 2) - (w_height / 2))
self.tk.geometry("{}x{}+{}+{}".format(w_width, w_height, x_coordinate, y_coordinate))
# 今日工作 明日计划
self.td_label.place(x=(w_width / 2 - 200) / 2, y=30, width=200, height=40)
self.tw_label.place(x=(w_width / 2 - 200) / 2 + w_width / 2, y=30, width=200, height=40)
# 内容
self.td_f.place(x=10, y=110, width=385, height=360)
self.td_sb.config(command=self.td_text.yview)
self.td_sb.pack(side=RIGHT, fill=Y)
self.td_text.pack(side="left")
self.td_text.insert(1.0, '1,')
self.tw_f.place(x=405, y=110, width=385, height=360)
self.tw_sb.config(command=self.tw_text.yview)
self.tw_sb.pack(side=RIGHT, fill=Y)
self.tw_text.pack(side="left")
self.tw_text.insert(1.0, '1,修改禅道已知的bug\n')
# 发送按钮
self.send_button.place(x=(w_width - 160) / 2, y=500, width=160, height=50)
# 功能函数
def click_send(self):
weeks = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
td_ct = self.td_text.get("1.0", 'end-1c').strip()
tw_ct = self.tw_text.get("1.0", 'end-1c').strip()
dtm = time.strftime("%Y-%m-%d", time.localtime())
wk = time.strftime("%w", time.localtime())
subject = '工作日志 {}—Android 王凯芳'.format(dtm)
message = '日期:{} {}\n今日工作内容:\n{}\n\n明日工作计划:\n{}'.format(dtm, weeks[int(wk)], td_ct, tw_ct)
message.replace('\n', '\r\n')
if not td_ct:
messagebox.showwarning(title='提示', message='今日工作内容是空的呀,摸鱼了?')
return
if not tw_ct:
messagebox.showwarning(title='提示', message='明日工作内容是空的呀,不干了?')
return
if td_ct and tw_ct:
self.send_mail(t_email=to_email, c_email=cc_email, subject=subject, message=message)
def send_mail(self, t_email, c_email, subject, message):
msg = EmailMessage()
msg['Subject'] = subject
msg['From'] = m_email
msg['To'] = ', '.join(t_email)
msg['Cc'] = ', '.join(c_email)
msg.set_content(message)
server = smtplib.SMTP(s_url)
server.login(m_email, e_pwd)
server.send_message(msg)
server.quit()
response = messagebox.askquestion("提示", "汇报成功,退出吗?", icon='warning')
if response == "yes":
self.tk.destroy()
f_name = '{}.txt'.format(time.strftime("%Y-%m-%d", time.localtime()))
f = open(f_name, "w", encoding='utf-8')
s = str(msg)
f.write(s)
f.close()
def start():
tk = Tk()
window = MyWindow(tk)
window.init_window()
tk.mainloop()
start()
保存为.pyw文件双击打开不显示cmd黑框。
发送成功自动保存当前发送内容到txt文件,在.pyw文件同目录下。