import mailbox import os from markdownify import markdownify # Configuration mbox_file = 'path/to/your/file.mbox' output_dir = 'path/to/output/directory' # Ensure output directory exists os.makedirs(output_dir, exist_ok=True) def save_email_as_markdown(email, index): subject = email.get('subject', 'No Subject') date = email.get('date', 'No Date') body = email.get_payload(decode=True).decode(errors='ignore') body_markdown = markdownify(body) # Create a Markdown file for each email filename = os.path.join(output_dir, f'email_{index}.md') with open(filename, 'w', encoding='utf-8') as file: file.write(f'# {subject}\n') file.write(f'*Date: {date}*\n\n') file.write(body_markdown) def convert_mbox_to_markdown(mbox_file): mbox = mailbox.mbox(mbox_file) for i, email in enumerate(mbox): save_email_as_markdown(email, i) convert_mbox_to_markdown(mbox_file)