import smtplib
from email.mime.multipartimport MIMEMultipart
from email.mime.textimport MIMEText
from email.headerimport Header
import os
from tkinterimport *
def initemail(emailadd,subject,name,filename,text,path,username,smtp):
receiver = emailadd
subject = Header(subject, 'utf-8').encode()
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['From'] ='Cai junjun(WABT)<{}>'.format(username)
msg['To'] = emailadd
text ="Hi {0}!\n\n{1}\n\n\nBest regards".format(name,text)
text_plain = MIMEText(text, 'plain', 'utf-8')
msg.attach(text_plain)
sendfile =open(path+'\\'+ filename, 'rb').read()
text_att = MIMEText(sendfile, 'base64', 'utf-8')
text_att["Content-Type"] ='application/octet-stream'
text_att.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(text_att)
smtp.sendmail(username, receiver, msg.as_string())
def GetFolderMsg(path):
# 从文件夹里获取工资条及邮箱跟姓名
L = []
D = {}
for root, dirs, filesin os.walk(path):
print(2323)
for filein files:
if file =="Config":
with open(root+"\\"+file,'r',encoding="utf-8")as f:
for iin f:
name_email = i.strip().split("_")
D[name_email[0]] = name_email[1]
else:
L.append(file)
# print(L,D)
return L,D
def main(path,text,subject,username,password):
smtp = smtplib.SMTP()
smtp.connect('smtp.exmail.qq.com')
smtp.login(username, password)
name,name_email = GetFolderMsg(path)
n =0
for k,vin name_email.items():
filename = k +".xlsx"
if filenamein name:
initemail(v,subject,k,filename,text,path,username,smtp)
name.remove(filename)
n +=1
return n,name
# path = r"E:\ME20200412182535"
# username = '#############'
# password = '#########'
# subject = "关于六月份工资问题"
# text = "感谢你为公司作出的"
# sender = username
#
#
#
# main(path, text, subject,username,password)
if __name__=="__main__":
title_01 ='''
君若扬路尘,妾若浊水泥,浮沈各异势,会合何时谐'''
def onButton():
password = password_.get()
username = username_.get()
path = Pic_path.get()# str 类型
text = fc_path.get()
subject = id_path.get()
try:
n,name = main(path, text, subject,username,password)
if name:
title_02 =' 发送成功{0}封,还有{1}没有发送请检查他们的邮箱跟姓名是否正确'.format(n,name)
Message(root, text=title_02, fg='red' , font=('楷体', 12 ), width=600).place(y=200)
elif n ==0:
title_05 =" 路径不正确请重新输入"
Message(root, text=title_05, fg='red', font=('楷体', 12), width=600).place(y=200)
else:
title_03 =" 发送成功,已发送{}封邮件".format(n)
Message(root, text=title_03, fg='blue', font=('楷体', 12), width=600).place(y=200)
except Exception:
title_04 =' 邮箱或密码不对请重新输入'
Message(root, text=title_04, fg='red', font=('楷体', 12), width=600).place(y=200)
# def func():
# global fd
# fd = str(r.get())
# if fd == "1":
# fd = "INTER"
# else:
# fd = "MOTOROLA"
root = Tk()
Message(root, text=title_01, font=('楷体', 12), width=600).place(y=200)
root.title('WABT')
# root.iconbitmap(r'E:\heronghuaPRJ\wechat\bitbug_favicon.ico')
root.geometry('800x400')
Label(root, text="邮箱:", font=('楷体', 18, "bold")).grid(row=1, column=2)
username_ = Entry(root, text="button", width=50, borderwidth=4)
username_.config(text="ddddd")
username_.grid(row=1, column=3)
Label(root, text="密码:", font=('楷体', 18, "bold")).grid(row=2, column=2)
password_ = Entry(root, text="button", width=50, borderwidth=4)
password_.config(text="eeeee")
password_.grid(row=2, column=3)
Label(root,text="文件夹路径:",font=('楷体', 18,"bold")).grid(row=3,column=2)
Pic_path = Entry(root,text="button",width=50,borderwidth=4)
Pic_path.config(text="aaaaa")
Pic_path.grid(row=3,column=3)
Label(root,text="邮件信息:",font=('楷体', 18,"bold")).grid(row=5,column=2)
fc_path = Entry(root,text="button",width=50,borderwidth=4)
fc_path.config(text="bbbbb")
fc_path.grid(row=5,column=3)
Label(root,text="邮件主题:",font=('楷体', 18,"bold")).grid(row=4,column=2)
id_path = Entry(root,text="button",width=50,borderwidth=4)
id_path.config(text="ccccc")
id_path.grid(row=4,column=3)
# Label(root,text="请输入帧的格式:",font=('楷体', 18,"bold")).grid(row=4,column=2)
# fd_path = Entry(root,text="button",width=50,borderwidth=4)
# fd_path.config(text="ddddd")
# fd_path.grid(row=4,column=3)
# r = IntVar()
# Radiobutton(root,text='INTER',font=('楷体', 18, "bold"),value=1,variable=r,command=func).grid(row=4,column=2)
# Radiobutton(root,text='MOTOROLA',font=('楷体', 18, "bold"),value=2,variable=r,command=func).grid(row=4,column=3)
# var = StringVar()
# Label(root, textvariable=var, bg='gray', fg='red', font=('楷体', 18,), width=30, height=2).grid(row=6,column=3)
btn = Button(root, text="发送邮件", font=('楷体', 18,"bold"),command=onButton).grid(row=6,column=3)
root.mainloop()