Last active
July 8, 2020 13:58
-
-
Save Morijarti/b827bc28914b964f9b7c428cf9108a05 to your computer and use it in GitHub Desktop.
Parse senders email information from eml files
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
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