postduif
postduif (Dutch for carrier pigeon) delivers your app's mail.
Async mail for Python web apps. postduif builds the MIME message (text and HTML, with images inside the mail), hands it to Postal over its HTTP API, and on a development machine writes it to an outbox instead, so nobody mails a real customer from a laptop.
- Never raises when sending.
send()returns aSendResult; what went wrong is in the log and in the result, never the API key. - Retries a temporary failure (HTTP 5xx or 429, no connection, a timeout) after 2 and 10 seconds; a refusal is not tried again.
- One message at a time to Postal, which answers simultaneous requests with a 500 now and then.
- Images inside the mail (
cid:), not linked from elsewhere: nothing to block, nothing that tells a server the mail was opened. - Attachments: files that come with the mail, a PDF say.
- Mail off is a Mailer too: no
if mailer is Nonein your app. redirect_tosends every mail to one address, to test with the real mail server.- Testing helpers: a mailer that records what it sends, and a mock of Postal's API.
Python 3.11 or later. Its only dependency is httpx.
pip install postduif
Sending a mail
from postduif import InlineImage, Mailer
mailer = Mailer.from_file("config.d/mail.json", outbox_path="data/outbox")
logo = InlineImage.from_file("static/logo.png", cid="logo@example.org")
result = await mailer.send(
to="ann@example.org",
subject="Your order",
text="Thank you for your order.",
html='<img src="cid:logo@example.org" alt=""><p>Thank you for your order.</p>',
reply_to="shop@example.org",
tag="order", # Postal's tag, to find mails of one kind
images=[logo],
)
if not result:
print(result.error) # also in the log; result.attempts says how often it was tried
Files come with a mail as attachments, with or without HTML:
from postduif import Attachment
invoice = Attachment.from_bytes(pdf_bytes, "invoice-2026-001.pdf") # or from_file(path)
await mailer.send(to="ann@example.org", subject="Your invoice", text="...",
attachments=[invoice])
send takes keywords only, so the fields can come from a dict: send(**fields).
mailer.send_in_background(...) takes the same and sends without making the caller wait and returns the asyncio
task. At shutdown, await mailer.aclose() waits for mails that are still going.
Links in a mail come from the configured base_url, never from a request:
mailer.link("/password-reset", token="a1b2") # https://example.org/password-reset?token=a1b2
Configuration
Mailer.from_file(path) reads a JSON file. Without the file, or with a file that is not
valid, mail is off and your app starts anyway; the log says why. If all your mails have links,
give require_base_url=True: without a base_url mail is off too.
{
"postal_url": "https://postal.example.org",
"api_key": "...",
"sender": "Example <noreply@example.org>",
"base_url": "https://example.org",
"timeout": 10
}
| Key | |
|---|---|
sender |
The From: of every mail. Required. |
transport |
"postal" (the default) or "outbox": no mail is sent, each mail becomes a .json and an .eml file in outbox_path. |
postal_url, api_key |
The Postal server and an API key of its mail server. Required for "postal". |
base_url |
Where your site is, for link(). Optional, unless require_base_url=True. |
timeout |
Seconds per request to Postal; default 10. |
redirect_to |
Send every mail to this address instead, with the real addressee in the subject. |
outbox_keep, outbox_days |
The outbox keeps the newest 50 mails, for at most 7 days. |
With "transport": "outbox", mailer.outbox is the OutboxTransport (else it is None), to
show the mails in your app: files() are their .json files, mails() their contents as dicts,
newest first, and Mail.from_dict(values) makes a Mail of one again, images included.
To build the configuration in code: MailConfig.from_dict(values) or MailConfig(...), then
make_mailer(config, outbox_path). Mailer.off() is a Mailer that sends nothing.
The subject of a redirected mail is "[test, for {to}] {subject}"; give
redirect_subject="..." to Mailer.from_file() or make_mailer() for another one.
Testing your app
from postduif import MailError
from postduif.testing import FakeMailer
async def test_order_confirmation():
mailer = FakeMailer()
await place_order(mailer, ...)
[mail] = mailer.mails # postduif Mail objects
assert mail.to == "ann@example.org"
mailer.clear() # forgets the mails sent so far
mailer.transport.fail_with(MailError("refused")) # the next mail fails
mailer.transport.fail_when(True) # every mail fails, until fail_when(None)
mailer.transport.fail_when(lambda mail: mail.to == "bob@example.org")
PostalMock plays Postal's API for a PostalTransport, with its answers and failures:
from postduif import Mailer, PostalTransport
from postduif.testing import PostalMock
postal = PostalMock()
postal.answer(PostalMock.http_error(500)) # first a temporary failure
transport = PostalTransport("https://postal.example.org", "key", httpx_transport=postal)
mailer = Mailer(transport, "noreply@example.org", retry_delays=(0, 0))
await mailer.send(to="ann@example.org", subject="Hello", text="text")
[_, sent] = postal.sent # rcpt_to, message, ...
Development
python3 -m venv env
env/bin/pip install -e . --group dev # pip >= 25.1
env/bin/pytest postduif
git config core.hooksPath .githooks # flake8, isort, black and the tests before a push
License
Metadata
Release files for postduif 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| postduif-0.3.0.tar.gz | 32.7 kB | With provenance | Details for postduif-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| postduif-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for postduif-0.3.0-py3-none-any.whl |
Total release size: 65.3 kB
Release files / postduif-0.3.0.tar.gz
| Download URL | postduif-0.3.0.tar.gz |
|---|---|
| Size | 32.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
52dc24937b0513a5efdd8751de8b3e25ba310548d7305522eca14152fe000fb9
|
|
BLAKE2b-256 checksum How to use checksums |
bc3f1f26eb8d28d455762628fbd34342678e72512798893ade1b9deb5d6da62b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logRelease files / postduif-0.3.0-py3-none-any.whl
| Download URL | postduif-0.3.0-py3-none-any.whl |
|---|---|
| Size | 32.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
463fecb51d64eab104349af228a5ccfb5085f81921c92cf1789e6c5b890b3a82
|
|
BLAKE2b-256 checksum How to use checksums |
e54d4e071d06d65d035b2b8d7a50ebf0d444f34444bd56ae47aba05990b6a76b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logSecurity for postduif 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for postduif-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.