背景:业务提出需求,希望数据部门可以开发一个小工具,帮助他们能够自动化下载某一特定主题邮件的附件,研究了很多种方法,记录下使用zmail+多线程的方式收取邮件。
import zmail
import queue
import threading
import pandas as pd
import os
from datetime import date, datetime, timedelta
q =queue.Queue()
results,threads,subject,ids,threads1=[],[],[],[],[]
tb=subject=pd.DataFrame()
run_time = datetime.strftime(datetime.now(), "%Y-%m-%d")
def get_subject_zmail(i):
server = zmail.server('xxxxx@.com','xxxxx',config='ali')
mail = server.get_headers(start_index=i,end_index=i+9) ####根据邮件索引取邮件heders信息
q.put(mail)
for i in range(0,2900,10):###2900是邮箱邮件数
t = threading.Thread(target=get_subject_zmail, args=(i,))
t.start()
threads.append(t)
for i in threads:
i.join()
while not q.empty():
results = results + q.get() ####把所有邮件头部存成列表
for index,result in enumerate(results): ###解析邮件主题并进行判断
value=result.get('Subject')
date=str(result.get('date')).split()[0]
if value.find('工资条')> -1 and date >= '2018-08-01': ###选择主题和时间范围
tb.loc[index,"id"]=result.get('id') ###拿到邮件的id值
tb.loc[index,"主题"]=value
tb.loc[index,"时间"]=date
print("邮件搜索成功")
def save_file(i):
server = zmail.server('xxxxx@.com','xxxxx',config='ali')
mail_file= server.get_mail(int(i))
attachment=mail_file.get('attachments') ###获取附件信息
if attachment:
if attachment[0][0].split(".")[1] in ("png","jepg","jpg"): ####指定附件类型
path = r"F:/work/test/file/{0}_email_file_{1}_1".format(int(i),run_time)
os.makedirs(path)
zmail.save_attachment(mail_file,target_path=path,overwrite=True)
if tb.shape[0]==0:
print("未查找到符合条件的邮件")
else:
for i in list(tb['id']):###传入符合条件的邮件id并保存附件
t1 = threading.Thread(target=save_file, args=(i,))
t1.start()
threads1.append(t1)
for i in threads1:
i.join()
print('附件保存成功')