Python-OXMSG

0.0.2 · active · verified Thu Apr 09

Python-OXMSG is a library designed to parse Outlook MSG (.msg) files to extract email messages and their attachments. Its primary use case focuses on extracting message text and accessing attachments, rather than modifying messages or creating them from scratch. The library also allows access to other message properties like sent-date. The current version is 0.0.2.

Warnings

Install

Imports

Quickstart

Loads a .msg file, accesses its properties like message class and attachment count, and demonstrates how to retrieve attachment details, including their content if available. Note that a real .msg file is required for actual parsing.

import os
from oxmsg import Message

# Create a dummy .msg file for demonstration purposes
# In a real scenario, you would have an actual .msg file
dummy_msg_content = b"""
This is a placeholder for a .msg file content.
In a real application, this would be binary data.
"""
with open("message.msg", "wb") as f:
    f.write(dummy_msg_content)

try:
    msg = Message.load("message.msg")
    print(f"Message Class: {msg.message_class}")
    print(f"Attachment Count: {msg.attachment_count}")

    if msg.attachment_count > 0:
        attachment = msg.attachments[0]
        print(f"  Attachment File Name: {attachment.file_name}")
        print(f"  Attachment MIME Type: {attachment.mime_type}")
        print(f"  Attachment Size: {attachment.size}")
        if attachment.attached_by_value:
            print(f"  Attachment Bytes (first 20): {attachment.file_bytes[:20]}...")
            # Example of saving attachment:
            # with open(attachment.file_name, "wb") as f:
            #     f.write(attachment.file_bytes)
        else:
            print("  Attachment bytes not available directly (attached by reference).")
    else:
        print("No attachments found.")

except Exception as e:
    print(f"Error processing MSG file: {e}")
finally:
    # Clean up the dummy file
    if os.path.exists("message.msg"):
        os.remove("message.msg")

view raw JSON →