บทนำ

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"])

การจัดเก็บ Compound File Binary (CFB)

ไลบรารีนี้รวมชั้น CFB แบบสแตนด์อโลน (CFBReader, CFBWriter, CFBDocument, CFBStorage, CFBStream) เพื่ออ่านและเขียนรูปแบบ Compound File Binary อย่างอิสระจาก 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 อนุญาตให้ใช้ในแอปพลิเคชันเชิงพาณิชย์โดยไม่มีค่าลิขสิทธิ์หรือข้อกำหนดการอ้างอิงนอกจากการรวมใบอนุญาต.


เริ่มต้นใช้งาน

แหล่งข้อมูลที่เกี่ยวข้อง