Skip to main content

Pure Python Outlook .msg file builder — no COM, no Outlook required

Project description

msgforge

Pure Python Outlook .msg file builder. No COM, no Outlook installation, no native dependencies. Works on Linux.

Experimental — this package implements the MS-CFB and MS-OXMSG specs from scratch. It has been tested with Outlook on Windows but may not cover all edge cases. Use at your own risk.

Install

pip install msgforge

Usage

from msgforge import Message

# Simple text email
msg = Message(
    subject="Hello",
    text_body="Hi there!",
    to=[("alice@example.com", "Alice")],
)
msg.save("hello.msg")

# HTML email with table, attachments, multiple recipients
msg = Message(
    subject="Weekly Report",
    html_body="""
    <p>Hi team,</p>
    <p>Please find the <b>weekly report</b> below:</p>
    <table border="1" style="border-collapse: collapse;">
        <tr><th style="padding: 4px 8px; background: #f0f0f0;">Region</th>
            <th style="padding: 4px 8px; background: #f0f0f0;">Revenue</th></tr>
        <tr><td style="padding: 4px 8px;">EMEA</td>
            <td style="padding: 4px 8px;">$1.2M</td></tr>
        <tr><td style="padding: 4px 8px;">APAC</td>
            <td style="padding: 4px 8px;">$800K</td></tr>
    </table>
    <p>Best regards</p>
    """,
    to=[("alice@example.com", "Alice"), ("bob@example.com", "Bob")],
    cc=[("carol@example.com", "Carol")],
)
msg.attach("report.xlsx")
msg.save("weekly_report.msg")

Features

  • HTML body — full CSS, tables, bold/italic rendering in Outlook (via encapsulated HTML in RTF)
  • Plain text body — fallback when no HTML provided
  • Recipients — TO, CC, BCC with display names
  • File attachments — appear in Outlook's attachment bar
  • Pure Python — only dependency is compressed-rtf
  • Cross-platform — works on Linux, macOS, Windows

API

Message(...)

Message(
    subject="...",           # Email subject
    html_body="<p>...</p>",  # HTML body (rendered in Outlook with full formatting)
    text_body="...",         # Plain text body (auto-generated from HTML if omitted)
    to=[("email", "Name")],  # TO recipients
    cc=[...],                # CC recipients
    bcc=[...],               # BCC recipients
)

Recipients can be ("email", "Name") tuples or just "email" strings.

.attach(path, filename=None)

Attach a file from disk. Returns self for chaining.

.attach_bytes(filename, data, mime_type=None)

Attach a file from bytes. MIME type auto-detected from extension.

.save(path)

Write the .msg file to disk.

.as_bytes()

Return the .msg file content as bytes (for web responses, etc).

Limitations

  • Experimental — implements MS-CFB and MS-OXMSG from scratch; not battle-tested across all Outlook versions
  • No read support — this package only creates .msg files, it cannot read or parse them (use extract-msg for that)
  • No embedded images — inline images (<img> tags referencing CID attachments) are not supported
  • No RTF body authoring — rich text is generated from HTML only; direct RTF input is not supported
  • No digital signatures or encryption — messages are unsigned and unencrypted
  • No calendar/contact/task items — only email messages (IPM.Note) are supported
  • ASCII-only RTF — non-ASCII characters in HTML bodies are encoded as RTF escapes, which may not render correctly for all scripts
  • Recipient resolution — recipients show display names but may not resolve to Exchange contacts until Outlook processes them
  • Large files — no streaming support; the entire message is built in memory

How it works

Implements two Microsoft specs from scratch:

  1. MS-CFB (Compound File Binary Format) — the OLE structured storage container
  2. MS-OXMSG (Outlook Item File Format) — MAPI properties, recipients, attachments

HTML bodies use the \fromhtml1 encapsulated HTML format, the same format Outlook uses internally. This gives full HTML/CSS rendering including tables, styling, etc.

Key implementation details discovered through testing:

  • Directory entry names must be sorted by length first, then case-insensitive (MS-CFB spec)
  • PT_UNICODE property sizes must include +2 bytes for the null terminator
  • HTML bodies require encapsulated HTML in RTF (\fromhtml1) — raw PR_HTML is not rendered by Outlook

Running tests

pip install .[dev]
pytest tests/

Built with AI

This package was built collaboratively with Claude Code (Claude Opus 4.6). The OLE compound file writer, MAPI property encoding, and encapsulated HTML format were implemented from the MS-CFB and MS-OXMSG specifications through iterative development and testing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

msgforge-0.1.1.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

msgforge-0.1.1-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file msgforge-0.1.1.tar.gz.

File metadata

  • Download URL: msgforge-0.1.1.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.5

File hashes

Hashes for msgforge-0.1.1.tar.gz
Algorithm Hash digest
SHA256 def30bad9fa45d39f09021149c6acc76b55ab633a2f2ce1360cef1c3e5f24eef
MD5 eb5b121e33f6ec6703476b650c23952b
BLAKE2b-256 ec17eac90d55a811ffe011616dfb818b8f323817a930b247bcb6bf2385dc8a91

See more details on using hashes here.

File details

Details for the file msgforge-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: msgforge-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.5

File hashes

Hashes for msgforge-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cbd6ae818ac2c10507d5830220377af98245b1ee2ffbe3c54d38a3506b7f2b9c
MD5 f73bbdb84a0c85bc22936a70f3acf6ce
BLAKE2b-256 e05cd4dbc501ad238cda40c8a3644094470a8bbfc860baab2299e6e67d47dd43

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page