Skip to main content

postduif

Tests PyPI

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 a SendResult; 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.
  • Mail off is a Mailer too: no if mailer is None in your app.
  • redirect_to sends 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

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

MIT, see LICENSE. Made by Seecr.

Metadata

Release files for postduif 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for postduif 0.2.0
File Size Uploaded Provenance Actions
postduif-0.2.0.tar.gz 31.1 kB With provenance Details for postduif-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for postduif 0.2.0
File Interpreter ABI Platform Actions
postduif-0.2.0-py3-none-any.whl Python 3 none any Details for postduif-0.2.0-py3-none-any.whl

Total release size: 62.3 kB

Release files / postduif-0.2.0.tar.gz

Download URL postduif-0.2.0.tar.gz
Size 31.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8f3562330e7262ac38e307ac3290cae8f6f0ef1256b2771a4d711b257cc20db6
BLAKE2b-256 checksum
How to use checksums
10e0a8f7ddc73b576a3dc543d704a79f63089d19627a2d8581844e209f48af72
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 13, 2026.

Transparency log

Release files / postduif-0.2.0-py3-none-any.whl

Download URL postduif-0.2.0-py3-none-any.whl
Size 31.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a4d1da9a0d02f5f20b16ee89e6ecf70152372b46ed15e7341fcf23487eeaf542
BLAKE2b-256 checksum
How to use checksums
43bb2142487281d30b3cd1896f738d24fa6676155441e765177d129a23c4ab29
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 13, 2026.

Transparency log

Security for postduif 0.2.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.

Select from 2 files

Attestation for postduif-0.2.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 13, 2026.

Transparency log

A 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.

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