Skip to content

Instantly share code, notes, and snippets.

@Morijarti
Last active July 8, 2020 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Morijarti/b827bc28914b964f9b7c428cf9108a05 to your computer and use it in GitHub Desktop.
Save Morijarti/b827bc28914b964f9b7c428cf9108a05 to your computer and use it in GitHub Desktop.
Parse senders email information from eml files
import argparse
import os
from email import policy
from email.parser import BytesParser
from pathlib import Path
def main():
parser = argparse.ArgumentParser()
parser.add_argument('eml_directory_path')
parser.add_argument('output_file')
args = parser.parse_args()
if not os.path.isdir(args.eml_directory_path):
raise IOError('EML directory not found. Please check given directory path')
eml_directory_path = Path(args.eml_directory_path)
senders = set()
for eml_file_path in eml_directory_path.rglob("*.eml"):
with open(eml_file_path, 'rb') as eml_file:
msg = BytesParser(policy=policy.default).parse(eml_file, headersonly=True)
senders.add(msg['From'])
# write all senders addresses
with open(args.output_file, 'w') as sender_file:
for sender in senders:
sender_file.write(sender)
sender_file.write('\n')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment