परिचय
Aspose.Email FOSS for Python (aspose-email-foss) एक MIT-लाइसेंस वाली Python लाइब्रेरी है जो Outlook MSG फ़ाइलों, RFC-5322 EML संदेशों, और Compound File Binary (CFB) स्टोरेज संरचनाओं के साथ काम करने के लिए है। लाइब्रेरी को कोई मूल एक्सटेंशन की आवश्यकता नहीं है और यह सीधे PyPI से स्थापित होती है।
यह लाइब्रेरी एक सामान्य Python विकास अंतर को दूर करती है: Outlook .msg फ़ाइलों को पढ़ना और लिखना बिना चल रहे Outlook इंस्टॉलेशन या स्वामित्व वाले SDK की आवश्यकता के। यह .msg फ़ाइलों के नीचे स्थित पूर्ण CFB कंटेनर फ़ॉर्मेट को भी उजागर करती है, जिससे यह संदेश माइग्रेशन से लेकर स्टोरेज निरीक्षण तक के कार्यों के लिए उपयुक्त बनती है।
यह लाइब्रेरी एक टाइप्ड 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-fossimport 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 लाइसेंसिंग व्यावसायिक अनुप्रयोगों में उपयोग की अनुमति देती है, बिना रॉयल्टी या लाइसेंस शामिल करने के अलावा किसी उल्लेख या एट्रिब्यूशन की आवश्यकता के।