Skip to content

Instantly share code, notes, and snippets.

@vadimkantorov
Created January 21, 2025 13:37

Revisions

  1. vadimkantorov created this gist Jan 21, 2025.
    17 changes: 17 additions & 0 deletions uneml.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # Usage: to extract all eml files in current directory into the current directory: python uneml.py *.eml

    import os
    import sys
    import email
    import email.policy
    for input_path in sys.argv[1:]:
    print('eml', repr(input_path))
    eml = email.message_from_file(open(input_path), policy = email.policy.default)
    for part in eml.walk():
    for attachment in part.iter_attachments():
    if not attachment.get_filename():
    continue
    output_filename = os.path.basename(input_path) + ' ' + attachment.get_filename()
    print('img', repr(output_filename))
    with open(output_filename, 'wb') as f:
    f.write(attachment.get_payload(decode = True))