from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
from email.mime.image import MIMEImage
from pathlib import Path
from email.mime.application import MIMEApplication
import os
import openpyxl as opxl
class EmailOpenFiles():
#開啟檔案
def __init__(self,DirectoryName,FileName):
self.DirectoryName = DirectoryName
self.FileName = FileName
def AttachFiles(self):
#取得附件資料夾內所有檔案名稱並回傳List
AttachDirPath=str(os.path.abspath(os.getcwd()))+'\\'+self.DirectoryName+'\\'
AttachFileNameList = os.listdir(AttachDirPath)
return AttachFileNameList
def EmailFileContent(self):
#設定信件內容檔名並取得檔案內容並回傳List
EmailContentFileName = str(os.path.abspath(os.getcwd()))+"\\"+self.FileName
EmailContentFile = open(EmailContentFileName,'r')
EmailContentList = EmailContentFile.readlines()
EmailContentFile.close()
return EmailContentList
def AttachDirPath(self):
#取得附件資料夾路徑
AttachDirPath=str(os.path.abspath(os.getcwd()))+'\\'+self.DirectoryName+'\\'
return AttachDirPath
def AttachExcelOpen(self):
#打開附件為Excel檔案並回傳List
AttachExcelFileName = str(os.path.abspath(os.getcwd()))+"\\"+self.FileName
TempExcelWorkBook = opxl.load_workbook(AttachExcelFileName)
TempMax_Row = TempExcelWorkBook.worksheets[0].max_row
TempMax_Column = TempExcelWorkBook.worksheets[0].max_column
TempReturnList = []
for i in range(1,TempMax_Row+1):
TempList = []
for j in range(1,TempMax_Column+1):
TempList.append(TempExcelWorkBook.worksheets[0].cell(i,j).value)
TempReturnList.append(TempList)
return TempReturnList
class PythonGmail():
def __init__(self,EmailSubject,EmailFrom,EmailTo,EmailContent,EmailAttachments,EmailSender,EmailPass):
self.EmailSubject = EmailSubject
self.EmailFrom = EmailFrom
self.EmailTo = EmailTo
self.EmailContent = EmailContent
self.EmailAttachments = EmailAttachments
self.EmailSender = EmailSender
self.EmailPass = EmailPass
def PythonGmail(self):
#建立MIMEMMultipart物件
content = MIMEMultipart()
#郵件標題
content["subject"] = self.EmailSubject
#寄件者Email
content["from"] = self.EmailFrom
#收件者Email
content["to"] = self.EmailTo
#郵件內容
content.attach(MIMEText(self.EmailContent))
#附件寄送檔案
AttachFileName = self.EmailAttachments
AttachFileload = MIMEApplication(open(AttachFileName,'rb').read())
AttachFileload.add_header('Content-Disposition','attachment',filename=AttachFileName)
content.attach(AttachFileload)
#設定SMTP伺服器
with smtplib.SMTP(host="smtp.gmail.com",port="587") as smtp:
try:
#驗證SMTP伺服器
smtp.ehlo()
#建立加密傳輸
smtp.starttls()
#登入寄件者Gmail
#smtp.login("寄件者信箱","寄件者密碼")
smtp.login(self.EmailSender,self.EmailPass)
#寄送郵件
smtp.send_message(content)
#顯示訊息
return "Success! Send OK!"
except Exception as e:
return "Error message:"+str(e)
#取得附件資料夾內所有檔案名稱
EmailAttachments = EmailOpenFiles('Mail','').AttachFiles()
#取得附件資料夾的絕對路徑
EmailAttachDirPath = EmailOpenFiles('Mail','').AttachDirPath()
#取得信件標題檔案內容並存成信件標題list
EmailSubjectlist = EmailOpenFiles('','信件標題.txt').EmailFileContent()
#從信件標題list取得信件標題字串
EmailSubject = EmailSubjectlist[0]
#取得信件內容檔案內容並存成信件內容list
EmailContentlist = EmailOpenFiles('','信件內文.txt').EmailFileContent()
#從信件內容list取得信件內容字串
EmailContent = ""
for i in EmailContentlist:
EmailContent = EmailContent + i
#從寄件者電郵與密碼.txt取得List
EmailSenderAddrList = EmailOpenFiles('','寄件者電郵與密碼.txt').EmailFileContent()
#去除List[0]標題
EmailSenderAddrList.pop(0)
#取得寄信者email與密碼
EmailSender,EmailPass = EmailSenderAddrList[0].split(',')
#從收件者資料.xlsx 取得List
EmailToList = EmailOpenFiles('','收件者資料.xlsx').AttachExcelOpen()
#去除List[0]標題
EmailToList.pop(0)
#未寄信者集合 tempSet
tempSet = set()
#依現有附件資料比對收件者資料,比對成功者逐一寄信
for i in EmailToList:
A1,A2,A3 = i
#將該員工編號加入未寄信者集合 tempSet
tempSet.add(int(A1))
for j in EmailAttachments:
if int(A1) == int(j[-9:-4]):
#EmailTest=PythonGmail(EmailSubject,EmailSender,A3,EmailContent,EmailAttachDirPath+j,EmailSender,EmailPass).PythonGmail()
#print(str(A1)+" "+str(A2)+" Email "+ EmailTest)
#將未寄信者集合tempSet移除該員工編號
tempSet.remove(int(A1))
# 將未寄信者資料寫入 未寄信者資料.txt
OutFileName = '未寄信者資料.txt'
f = open(OutFileName,'w')
for i in tempSet:
for j in EmailToList:
A1,A2,A3 = j
if int(i) == int(A1):
print(str(A1)+" "+str(A2)+" "+str(A3),file=f)
f.close()
os.system("pause")
os.system("exit")