מבוא

Aspose.Email FOSS עבור Python (aspose-email-foss) היא ספרייה Python ברישיון MIT לעבודה עם קבצי Outlook MSG, הודעות RFC-5322 EML, ומבני אחסון Compound File Binary (CFB). הספרייה אינה דורשת הרחבות מקומיות ומותקנת ישירות מ-PyPI.

הספרייה מתמודדת עם פער פיתוח Python נפוץ: קריאה וכתיבה של קבצי Outlook .msg ללא צורך בהתקנת Outlook פעילה או SDK קנייני. היא גם חושפת את פורמט המכולה המלא CFB המהווה בסיס לקבצי .msg, מה שהופך אותה למתאימה למשימות המשתרעות ממחזור הודעות ועד בדיקת אחסון.

הספרייה מגיעה עם ממשק API טיפוסי המכסה בניית הודעות MAPI, ניהול נמענים וקבצים מצורפים, סריאליזציית מאפיינים, ונסיעות חוזרות של מסמכי CFB.


תכונות מרכזיות

יצירת וקידוד קבצי MSG

MapiMessage.create() בונה הודעת MAPI חדשה משורת נושא וגוף. משם ניתן להוסיף מאפיינים טיפוסיים, נמענים וקבצים מצורפים בינריים, ולאחר מכן לסריאליז את התוצאה לבייטים באמצעות to_bytes().

import datetime
from aspose.email_foss.msg.mapi_message import MapiMessage, PropertyId

message = MapiMessage.create("Quarterly Report", "Please review the attached report.")
message.set_property(PropertyId.SENDER_NAME, "Alice")
message.set_property(PropertyId.SENDER_EMAIL_ADDRESS, "alice@example.com")
message.set_property(
    PropertyId.MESSAGE_DELIVERY_TIME,
    datetime.datetime(2026, 3, 15, 10, 30, tzinfo=datetime.timezone.utc),
)
message.add_recipient("bob@example.com", display_name="Bob")
message.add_attachment("report.pdf", b"%PDF-1.4 ...", mime_type="application/pdf")

raw_bytes = message.to_bytes()

קריאת קבצי MSG קיימים

MapiMessage.from_file() קורא קובץ .msg קיים מהדיסק. ניתן גם לשחזר הודעה מבייטים גולמיים על-ידי העברת CFBReader דרך MsgReader ו-MsgDocument.

from aspose.email_foss.msg.mapi_message import MapiMessage
from aspose.email_foss.msg.reader import MsgReader, MsgDocument
from aspose.email_foss.cfb.reader import CFBReader

# Read from file path
msg = MapiMessage.from_file("message.msg")
print(msg.subject)

# Or reconstruct from bytes
msg2 = MapiMessage.from_msg_document(
    MsgDocument.from_reader(MsgReader(CFBReader(raw_bytes)))
)
print(msg2.subject)
for att in msg2.attachments:
    print(att.filename, len(att.data), "bytes")

המרה בין MAPI ל-RFC-5322 EML

MapiMessage.from_email_message() ממיר אובייקט Python email.message.EmailMessage סטנדרטי למסר MAPI, תוך שמירה על נושא, נמענים, גוף והקבצים המצורפים. ההמרה ההפוכה משתמשת ב-to_email_message().

from email.message import EmailMessage
from aspose.email_foss.msg.mapi_message import MapiMessage

# Build a standard Python email
eml = EmailMessage()
eml["Subject"] = "Meeting notes"
eml["From"] = "alice@example.com"
eml["To"] = "bob@example.com"
eml.set_content("Notes from today's meeting.")

# Convert to MAPI and serialize as .msg
mapi = MapiMessage.from_email_message(eml)
with open("meeting-notes.msg", "wb") as f:
    f.write(mapi.to_bytes())

# Round-trip back to EML
recovered = mapi.to_email_message()
print(recovered["Subject"])

אחסון קובץ משולב בינארי (CFB)

הספרייה כוללת שכבת CFB עצמאית (CFBReader, CFBWriter, CFBDocument, CFBStorage, CFBStream) לקריאה וכתיבה של פורמט קובץ משולב בינארי באופן בלתי תלוי ב-MAPI. זה שימושי לבדיקת המבנה הגולמי של קבצי .msg או לבניית מכולות קבצים משולבות מותאמות.

from aspose.email_foss.cfb.writer import CFBWriter, CFBDocument, CFBStorage, CFBStream
from aspose.email_foss.cfb.reader import CFBReader

# Build a CFB document with nested storage
root = CFBStorage("Root Entry")
root.add_stream(CFBStream("Metadata", b"meta-payload"))
nested = root.add_storage(CFBStorage("Attachments"))
nested.add_stream(CFBStream("Attachment1", b"binary-content"))

doc = CFBDocument(root=root, major_version=3)
data = CFBWriter.to_bytes(doc)

# Read it back
reader = CFBReader(data)
entry = reader.resolve_path(["Attachments", "Attachment1"])
print(reader.get_stream_data(entry.stream_id))

גישה למאפייני MAPI

iter_properties() מחזיר את כל מאפייני MAPI על הודעה כאובייקטים MapiProperty. מאפיינים בשם (PidLidTag ו-PidNameTag) נגישים דרך MapiNamedProperty. MapiPropertyCollection מספק גישה למאפיינים בדומה למילון.

from aspose.email_foss.msg.mapi_message import MapiMessage

msg = MapiMessage.from_file("message.msg")
for prop in msg.iter_properties():
    print(hex(prop.property_id), prop.property_type, repr(prop.value)[:60])

התחלה מהירה

pip install aspose-email-foss
import datetime
from aspose.email_foss.msg.mapi_message import MapiMessage, PropertyId
from aspose.email_foss.msg.reader import MsgReader, MsgDocument
from aspose.email_foss.cfb.reader import CFBReader

# Create a message
message = MapiMessage.create("Hello", "This is the body.")
message.set_property(PropertyId.SENDER_NAME, "Sender")
message.set_property(PropertyId.SENDER_EMAIL_ADDRESS, "sender@example.com")
message.add_recipient("recipient@example.com", display_name="Recipient")
message.add_attachment("note.txt", b"attachment content", mime_type="text/plain")

# Serialize and reload
raw = message.to_bytes()
reloaded = MapiMessage.from_msg_document(
    MsgDocument.from_reader(MsgReader(CFBReader(raw)))
)
email = reloaded.to_email_message()
print("Subject:", email["Subject"])
print("From:", email["From"])

פורמטים נתמכים

פורמטסיומתקריאהכתיבה
הודעה.msg✓✓
CFB.cfb✓✓
eml.eml✓✓

קבצי MSG נקראים דרך MapiMessage.from_file() ו-MsgReader. גישה לקבצי EML מתבצעת באמצעות גשר ההמרה from_email_message() / to_email_message(). מכולות CFB מנוהלות ישירות על ידי CFBReader ו-CFBWriter.


קוד פתוח ורישוי

Aspose.Email FOSS עבור Python שוחרר תחת רישיון MIT. המקור זמין ב-GitHub והחבילה מופצת דרך PyPI תחת השם aspose-email-foss. רישיון MIT מאפשר שימוש ביישומים מסחריים ללא תמלוגים או דרישות ייחוס מעבר לכלילת הרישיון.


התחלה

משאבים קשורים