Python-OXMSG
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
- gotcha The library explicitly states that it does not support modifying or creating Outlook MSG files. Its functionality is limited to parsing and extracting data.
- gotcha When accessing attachment content, the `attachment.file_bytes` property is only populated if the attachment's `attached_by_value` property is `True`. If `attached_by_value` is `False`, the bytes are not directly available through this property, indicating it's an attachment by reference (e.g., a linked file).
- breaking Python-OXMSG requires Python 3.9 or higher. Users running older Python versions will encounter installation or runtime errors.
Install
-
pip install python-oxmsg
Imports
- Message
from oxmsg import Message
Quickstart
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")