Skip to main content

MailFlat — Python SDK

Official Python client for MailFlat: disposable, automation-friendly email inboxes with one-line OTP retrieval. Spin up a real inbox, read the verification code your app just sent, and move on — one call instead of a hand-rolled poll loop, no shared mailbox state.

pip install mailflat

Quickstart

from mailflat import MailFlat

mf = MailFlat(api_key="mf_live_...")  # or set MAILFLAT_API_KEY

# 1 · spin up a disposable inbox
inbox = mf.create(prefix="signup-test", label="checkout flow")
print(inbox.address)            # → signup-test@x7k2m.mailflat.net

# 2 · your app/browser submits the form using inbox.address ...

# 3 · grab the OTP (polls until it arrives or times out)
otp = inbox.wait_for_otp(timeout=30)
print(otp)                      # → "123456"

# 4 · answering a human keeps the same email thread
msg = inbox.wait_for_message(timeout=60)
msg.reply("thanks, received!")

# inbox auto-clears in 2h — no cleanup needed (or call inbox.delete())

For AI agents

Hand an agent one API key and it spins up real inboxes on demand:

mf = MailFlat()  # reads MAILFLAT_API_KEY

inbox = mf.create(prefix="deep-research")
browser.fill("#email", inbox.address)
browser.click("Sign up")

otp = inbox.wait_for_otp(timeout=30)
browser.fill("#code", otp)

API

MailFlat(api_key=None, *, base_url="https://mailflat.net", timeout=30.0, max_retries=2, http_client=None)

Client. api_key falls back to the MAILFLAT_API_KEY environment variable. Use base_url for self-hosted / BYOD deployments. Supports use as a context manager (with MailFlat() as mf:). Pass http_client= an httpx.Client to inject your own transport — this is how you drive the SDK in tests without a network (e.g. httpx.MockTransport).

  • create(*, prefix=None, label=None, subdomain=None, domain=None, retention_hours=None) -> Inbox — open a new inbox. create_inbox(...) is an alias. prefix shapes the address; label does not. prefix="signup-test" gives you signup-test@…, while label is only a name shown in the dashboard so you can tell your inboxes apart. Omitting prefix yields a random agent-… address.
  • list() -> list[Inbox] — inboxes opened with this key.
  • inbox(address) -> Inbox — attach to an existing address without a network call.

Inbox

  • .address — the email address.
  • .messages(*, direction="in") -> list[Message] — messages, newest first.
  • .latest(*, direction="in") -> Message | None — most recent message.
  • .wait_for_otp(*, timeout=30, poll_interval=1.0) -> str — poll until an OTP arrives; returns the code.
  • .wait_for_message(*, timeout=30, poll_interval=1.0, direction="in") -> Message — poll until a message arrives.
  • .send(to, *, subject="", body="", html=None, in_reply_to=None) -> dict — send a DKIM-signed email from this inbox. Pass in_reply_to (a Message-ID) to stay in a thread.
  • .mark_read(message_id) -> dict — mark a message read so later polls can skip it.
  • .burn() -> dict — delete every message but keep the address.
  • .download_attachment(message_id, attachment_id) -> bytes — fetch an attachment's bytes.
  • .delete() -> dict — delete the inbox and all its messages.

Reads return received mail by default. direction="out" returns mail you sent from this address, "all" returns both. This matters for agent-to-agent flows: without it, send() followed by wait_for_message() returns your own outgoing message.

Message

.otp, .subject, .sender, .text, .html, .to_address, .direction, .received_at, .links, .attachments, .spam, .headers, .is_read, .raw.

  • .links — URLs found in the body (HTML hrefs first). For "click the verification link" flows.
  • .attachments — metadata; msg.attachments[0].download() fetches the bytes. ⚠️ send() cannot attach files yet, so you cannot produce an attachment from the SDK alone — send one from a normal mail client to test this path.
  • .spam{score, required, is_spam, rules, scanner}, or None when never scanned (which is not the same as a score of 0). ⚠️ Spam scanning is currently disabled on mailflat.net, so today this is None for every message.
  • .headers — raw headers, exactly as they arrived. Names are case-insensitive per RFC 5322 but this is a plain dict, so do not index it: the real key is Message-ID, and headers["Message-Id"] raises KeyError. Use .header("message-id") or .message_id.
  • .reply(body, *, html=None, subject=None) — answer this message in the same conversation. Fills in the recipient, an Re: subject and the In-Reply-To / References headers Gmail and Outlook thread on. A hand-rolled send() starts a new conversation instead, which does not look like a reply.
  • .mark_read() / .delete() — act on this message directly.

Errors

All errors subclass MailFlatError: AuthenticationError (401), MailFlatPermissionError (403, still exported as PermissionError for compatibility — the new name no longer shadows the built-in), NotFoundError (404), RateLimitError (429, carries .retry_after when the server sent one), APIError (other), OTPTimeoutError (no OTP before timeout), EncryptedInboxError (the inbox is end-to-end encrypted, so the server cannot read its contents — use a non-encrypted inbox for agent automation).

License

MIT

Download files

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

Source Distribution

mailflat-0.6.0.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

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

mailflat-0.6.0-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file mailflat-0.6.0.tar.gz.

File metadata

  • Download URL: mailflat-0.6.0.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.9

File hashes

Hashes for mailflat-0.6.0.tar.gz
Algorithm Hash digest
SHA256 c82c3b072c85dbf4ad645bd228eb04c8ba97602a12c61fadf0e480b4734551dc
MD5 7ff7573c52746d4c3db0f581221deb45
BLAKE2b-256 60f68ec5db335286607ac638b4030c6016b7f29d89c760d89a3e045ac753c2f9

See more details on using hashes here.

File details

Details for the file mailflat-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: mailflat-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 20.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.9

File hashes

Hashes for mailflat-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0a8d51d45a2aafa32fb66f71392d710d1b731bfd066330eacc96e773947fd9e7
MD5 d30227ca26bac41e5c9b979266440e9c
BLAKE2b-256 06374792774445da5f79f719f29328515b32c62bbcbf11d96d5c44be86e8cd99

See more details on using hashes here.

Release history Release notifications | RSS feed

0.11.0

2 files

0.10.2

2 files

0.10.1

2 files

0.10.0

2 files

0.9.0

2 files

0.8.0

2 files

0.7.0

2 files

0.6.1

2 files

This release

0.6.0 This release

2 files

0.5.0

2 files

0.4.1

2 files

0.4.0

2 files

0.3.3

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

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