Last active
October 4, 2020 20:20
-
-
Save ag-michael/9eaba6680f7c10b71380c13bcfefb76a to your computer and use it in GitHub Desktop.
Recursively dump parsed eml file information into a single text file for analysis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
# Requirement: run python -m pip install eml_parser | |
# Syntax: python.exe .\dump_eml.py . .\dumpfile.txt | |
import os,sys,datetime | |
import eml_parser,json | |
separator = "\\" | |
def json_serial(obj): | |
if isinstance(obj, datetime.datetime): | |
serial = obj.isoformat() | |
return serial | |
ep = eml_parser.EmlParser(include_raw_body=True,include_attachment_data=True) | |
with open(sys.argv[2],"wb+") as output: | |
def dump(d,f): | |
with open(d+separator+f,"rb") as eml: | |
try: | |
output.write(json.dumps(ep.decode_email_bytes(eml.read()),default=json_serial,indent=4,sort_keys=True).encode("utf-8")) | |
except Exception as e: | |
print(e) | |
print("Error with:"+d+separator+f) | |
output.write(("\n+"+("-"*80)+"+\n").encode("utf-8")) | |
for directory, dirnames, filenames in os.walk(sys.argv[1]): | |
for file in filenames: | |
if file.lower().endswith(".eml"): | |
dump(directory,file) | |
print("Finished: "+directory+separator+file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment