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()