import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
import os,time
from tkinter import *
def initemail(emailadd,subject,name,filename,text,username,smtp,user_h):
receiver = emailadd
subject = Header(subject, 'utf-8').encode()
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['From'] = '{0}(WABT)<{1}>'.format(user_h,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)
with open(filename, 'rb') as f:
sendfile = f.read()
text_att = MIMEText(sendfile, 'base64', 'utf-8')
text_att["Content-Type"] = 'application/octet-stream'
text_att.add_header('Content-Disposition', 'attachment', filename="工资条.xlsx")
msg.attach(text_att)
smtp.sendmail(username, receiver, msg.as_string())
def GetFolderMsg(path):
# 从文件夹里获取工资条及邮箱跟姓名
D = {}
for root, dirs, files in os.walk(path):
for file in files:
if file == "Config":
with open(root+"\\"+file,'r',encoding="utf-8") as f:
for i in f:
if len(i) > 4:
name_email = i.strip().split("_")
path_name =name_email[0]+".xlsx"
if path_name in files:
D[name_email[0]] = (name_email[1],root+"\\"+path_name)
return D
def main(path,text,subject,username,password,user_h):
smtp = smtplib.SMTP()
smtp.connect('smtp.exmail.qq.com')
smtp.login(username, password)
name_email = GetFolderMsg(path)
ab = len(name_email)
c = ab
n = 0
for k,v in name_email.items():
initemail(v[0],subject,k,v[1],text,username,smtp,user_h)
ab -=1
n += 1
change_schedule(n, c)
time.sleep(1)
return n,ab
# path = r"E:\ME20200412182535"
# username = 'ronghua.he@wabertec.com'
# password =
自动发邮件脚本
最新推荐文章于 2024-05-07 02:49:53 发布