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, Mail, 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(
    Mail(
        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

The short form does the same: await mailer.send(to, subject, text, html, tag="order"). mailer.send_in_background(...) 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.

{
    "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.
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.

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.transport.fail_with(MailError("refused"))   # the next mail fails

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("ann@example.org", "Hello", "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.1.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.1.0
File Size Uploaded Provenance Actions
postduif-0.1.0.tar.gz 28.9 kB With provenance Details for postduif-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 58.2 kB

Release files / postduif-0.1.0.tar.gz

Download URL postduif-0.1.0.tar.gz
Size 28.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a7e7b8285e608233f0974f53715f807312d1e2aaef487ea95b4e15badfbb33fe
BLAKE2b-256 checksum
How to use checksums
ff8e6e3c393a42bd9efd2e92d2c3377cb848826dc8d4c36bde79ec7346b5dd3b
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.1.0-py3-none-any.whl

Download URL postduif-0.1.0-py3-none-any.whl
Size 29.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eff023398e1ee7f350d64c90d2277aa90a36477524af6465b1c5e7a7e3d1e7df
BLAKE2b-256 checksum
How to use checksums
bb788dc99b428d1bc8e93ee5e486c5154b15a10f7c1ff5fd2e2a00af781d60e0
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.1.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.1.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