coremail导出用户草稿箱中的邮件并打包
作者:互联网
#!/usr/bin/python # -*- coding:utf-8 -*- import subprocess import datetime import re import os import tarfile import time # 邮件导出 目录结构说明 # /home/coremail/ExportUserDraftBoxEml # --> user@domain目录 # ------> eml格式的文件 exportEmlDir = '/home/coremail/ExportUserDraftBoxEml' nowDateTime = datetime.datetime.now().strftime("%Y%m%d") loggerFile = '/tmp/exportEmlForDraftBox.log.%s' % nowDateTime # 日志 def logger(context): with open(loggerFile, 'a+') as f: f.write(context + '\n') f.flush() print context # 压缩 生成list标志文件 上传压缩包 上传list文件 删除exportEmlDir目录 def compressFile(): compressfilename = 'MAIL_EML_RJZX_%s.tar.gz' % nowDateTime compressfilefullpath = '/home/coremail/%s'% compressfilename # print compressfilename # print compressfilefullpath logger('[-] compress file %s' % compressfilefullpath) with tarfile.open(compressfilefullpath, 'w:gz') as tar_f: tar_f.add(exportEmlDir) # 生成list标志文件 压缩包名字 和 文件大小 listFile = '/home/coremail/MAIL_EML_RJZX_%s.list' % nowDateTime with open(listFile, 'w') as f: f.write('%s: %sKB' % (compressfilename, os.stat(compressfilefullpath).st_size) + '\n') # 5秒后再删除文件 time.sleep(5) if os.path.isdir(exportEmlDir): logger('[-] delete directories file %s' % exportEmlDir) # shutil.rmtree(exportEmlDir) if os.path.isfile(compressfilefullpath): logger('[-] delete tar file %s' % compressfilefullpath) # os.remove(compressfilefullpath) print '[+] list file save to %s' % listFile # 列出用户草稿箱的邮件 def listUserDraftBoxMail(coremailuser): msgYesterdayDate = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime("%Y%m%d") list_msg = '/home/coremail/bin/userutil --list-msg %s \'fid=2&time=%s\'' % (coremailuser, msgYesterdayDate) logger(list_msg) mailListResult = subprocess.Popen(list_msg, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for line in mailListResult.stdout.readlines(): # print line mid = re.search("1tb[a-zA-Z0-9\+\-\*\/]{19}", line) if mid: logger(line.strip()) exportUserDraftBoxMail(coremailuser.strip(), mid.group().strip()) # 导出用户草稿箱中的邮件 def exportUserDraftBoxMail(coremailuser, mid): userEmlDir = os.path.join(exportEmlDir, coremailuser) print userEmlDir if not os.path.isdir(userEmlDir): logger('[-] create %s directories' % userEmlDir) os.mkdir(userEmlDir) # 创建保存用户邮件的目录 # print coremailuser # print mid filterMid = re.sub('[\+\-\*\/]', '', mid) # print filterMid emlFileName = os.path.join(userEmlDir, filterMid) dump_msg = '/home/coremail/bin/userutil --dump-msg %s %s>%s.eml' % (coremailuser, mid, emlFileName) logger(dump_msg) os.system(dump_msg) # orgID = ['a', 'b', 'c'] # 根据组织id中的用户导出邮件 orgUnitId = ['100002345'] # 部门id if __name__ == '__main__': if not os.path.isdir(exportEmlDir): os.mkdir(exportEmlDir) # 创建邮件保存的目录 # listUserDraftBoxMail('admin@leejay.com.cn') # 查找组织ID中的用户传入方法 for orgline in orgUnitId: # 列出某个组织下面的所有用户 # select_user = '/home/coremail/bin/userutil --select-user @\'org_id="%s"\'' % orgline # 列出a组织下,某个部门的所有用户 select_user = '/home/coremail/bin/userutil --call-api "cmd=302&org_id=a&org_unit_id=%s&scope=2"|sed "s/Struct/\\n/g"|awk -F\'un=|,dn=|,dd=\' \'{print $2"@"$3}\'|grep -v "^@"' % orgline logger(select_user) userlistRelust = subprocess.Popen(select_user, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for userline in userlistRelust.stdout.readlines(): # print userline.strip() listUserDraftBoxMail(userline.strip()) # 打包压缩 compressFile() print '[+] user mail save to %s' % exportEmlDir print '[+] log save to %s' % loggerFile
标签:exportEmlDir,list,coremail,草稿箱,print,logger,os,邮件 来源: https://blog.51cto.com/11732619/2677012