Skip to content

Instantly share code, notes, and snippets.

@schroneko
Created December 30, 2024 10:24
Show Gist options
  • Select an option

  • Save schroneko/6af1d78825d271897a7426f5537cf6b0 to your computer and use it in GitHub Desktop.

Select an option

Save schroneko/6af1d78825d271897a7426f5537cf6b0 to your computer and use it in GitHub Desktop.
import email
import os
from email import policy
from email.parser import BytesParser
def extract_email_content(eml_file):
"""Extract plain text content from email file."""
with open(eml_file, 'rb') as f:
msg = BytesParser(policy=policy.default).parse(f)
# Get plain text content
if msg.is_multipart():
for part in msg.iter_parts():
if part.get_content_type() == 'text/plain':
return part.get_content()
else:
if msg.get_content_type() == 'text/plain':
return msg.get_content()
return None
def process_emails():
"""Process all .eml files and create markdown files."""
# Get all .eml files in current directory
eml_files = [f for f in os.listdir('.') if f.endswith('.eml')]
for eml_file in eml_files:
# Extract content
content = extract_email_content(eml_file)
if content:
# Create markdown filename
md_filename = os.path.splitext(eml_file)[0] + '.md'
# Write content to markdown file
with open(md_filename, 'w', encoding='utf-8') as f:
f.write(content)
print(f"Processed: {eml_file} -> {md_filename}")
if __name__ == '__main__':
process_emails()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment