Skip to main content

MailCue Python SDK

Official Python client for MailCue — the open-source email testing and production server (Postfix + Dovecot + FastAPI + React) packaged as one Docker container.

The SDK is the same in dev and prod: point it at http://localhost:8088 while you build, then swap base_url to your production MailCue deployment when you ship.

Install

pip install mailcue

Requires Python 3.9+.

Quick start: send an email

from mailcue import Mailcue

client = Mailcue(api_key="mc_xxx")  # base_url defaults to http://localhost:8088

result = client.emails.send(
    from_="hello@example.com",
    to=["user@example.com"],
    subject="Welcome",
    html="<h1>Hi there</h1>",
)
print(result.message_id)

Need async? Use AsyncMailcue — same surface, all methods become coroutines:

import asyncio
from mailcue import AsyncMailcue


async def main() -> None:
    async with AsyncMailcue(api_key="mc_xxx") as client:
        await client.emails.send(
            from_="hello@example.com",
            to=["user@example.com"],
            subject="Welcome",
            html="<h1>Hi there</h1>",
        )


asyncio.run(main())

Listing an inbox

inbox = client.emails.list(mailbox="user@example.com", page_size=20)
for email in inbox.emails:
    print(email.uid, email.subject, email.from_address)

detail = client.emails.get(inbox.emails[0].uid, mailbox="user@example.com")
print(detail.text_body)

Waiting for an email (CI)

wait_for polls a mailbox until matching messages arrive, or raises mailcue.TimeoutError after timeout seconds. Filters (subject, from_address, to_address) are case-insensitive substrings on top of the server-side search.

found = client.emails.wait_for(
    mailbox="user@test.com",
    subject="Welcome",
    timeout=10,
)
assert len(found) == 1

Attachments

attachments accepts raw bytes, str, or a pathlib.Path. The SDK base64-encodes the content for you.

from pathlib import Path

client.emails.send(
    from_="hello@example.com",
    to=["user@example.com"],
    subject="Invoice",
    html="<p>See attached.</p>",
    attachments=[
        {
            "filename": "invoice.pdf",
            "content_type": "application/pdf",
            "content": Path("./invoice.pdf"),
        }
    ],
)

Real-time events (SSE)

for event in client.events.stream():
    print(event.event_type, event.data)

The async version:

async with AsyncMailcue(api_key="mc_xxx") as client:
    async for event in client.events.stream():
        print(event.event_type, event.data)

The SSE client auto-reconnects with exponential backoff if the connection drops.

Error handling

from mailcue import (
    Mailcue,
    AuthenticationError,
    NotFoundError,
    RateLimitError,
    ValidationError,
)

client = Mailcue(api_key="mc_xxx")

try:
    client.emails.get("not-a-real-uid", mailbox="user@example.com")
except NotFoundError as exc:
    print("missing:", exc)
except RateLimitError as exc:
    print(f"slow down; retry after {exc.retry_after}s")
except AuthenticationError:
    print("bad API key")
except ValidationError as exc:
    print("server rejected the request:", exc.detail)

Configuration

client = Mailcue(
    api_key="mc_xxx",  # or bearer_token="eyJ..."
    base_url="https://mail.example.com",  # default: http://localhost:8088
    timeout=30.0,  # seconds
    max_retries=3,  # 502/503/504 + network errors
    verify=True,  # set False for self-signed dev TLS
)

You can also inject your own httpx.Client / httpx.AsyncClient via http_client= for advanced cases (custom transports, proxies, mTLS).

Resources

Resource Methods
client.emails send, list, get, get_raw, get_attachment, delete, inject, bulk_inject
client.mailboxes list, create, delete, stats, purge, list_emails
client.domains list, create, get, verify_dns, delete
client.aliases list, create, get, update, delete
client.gpg list, generate, get, export_public, import_key, publish, delete
client.api_keys list, create, delete
client.system health, get_certificate, download_certificate, settings, tls_status
client.events stream() (SSE iterator)

License

MIT — see LICENSE.

Project home: https://github.com/Olib-AI/mailcue

Download files

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

Source Distribution

mailcue-0.1.4.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

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

mailcue-0.1.4-py3-none-any.whl (26.8 kB view details)

Uploaded Python 3

File details

Details for the file mailcue-0.1.4.tar.gz.

File metadata

  • Download URL: mailcue-0.1.4.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for mailcue-0.1.4.tar.gz
Algorithm Hash digest
SHA256 41c5eeb3bdb9dc60b90f1aba8d95cbb0b2d08032ee8eecf3343afe886e6aba42
MD5 09db6e11a03cca3ce40ae39e148fa729
BLAKE2b-256 7c6314204e16c8f315659336ed2c193be15c60f7c76bd1670f517cc03c638a41

See more details on using hashes here.

Provenance

The following attestation bundles were made for mailcue-0.1.4.tar.gz:

Publisher: sdk-publish.yml on Olib-AI/mailcue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mailcue-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: mailcue-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 26.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for mailcue-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 062a7e174bb67bf65410375008350af2fb3d4811e3525c48564129584e21f266
MD5 fa52b38a24d4e6f94bed23cfa585a59e
BLAKE2b-256 f905e1850f7d5b3d9dcbd957f94a0d84c61dec2f95aac8e5e5908888c7cf8c7a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mailcue-0.1.4-py3-none-any.whl:

Publisher: sdk-publish.yml on Olib-AI/mailcue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.2.0

2 files

0.1.6

2 files

0.1.5

2 files

This release

0.1.4 This release

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page