stapel-mailtrap
An email trap (mail catcher) for dev/staging: outbound mail is captured into a TrappedEmail journal instead of being sent, and browsed through a read-only, scope-filtered Mail API. Plugs into stapel-notifications' email-provider seam by dotted path (duck-typed send() contract) — no module-to-module import.
Part of the Stapel framework — composable Django apps that deploy as a monolith or as microservices without changing module code.
Install
pip install stapel-mailtrap
At a glance
| Fact | Value |
|---|---|
| Version | 0.2.0 |
| Python | >=3.11 (3.11, 3.12, 3.13, 3.14) |
| HTTP operations | 2 |
| Config axes | 2 |
| Usage surface | 3 |
| Extension points | 3 |
| Error codes | 44 |
| Fleet dependencies | stapel-core · stapel-notifications (optional) |
Documentation
OpenAPI · capabilities.json · llms.txt (for agents)
What this is
On staging, every outbound email is a small hazard: it can reach a real address, and it carries OTPs and magic links. The usual answers are to disable mail (and lose the ability to test anything that sends it) or to point staging at a third-party catcher (and hand it your one-time codes).
stapel-mailtrap is the third answer: outbound mail is captured into a
TrappedEmail journal inside your own database and browsed through a
read-only Mail API. Nothing leaves the deployment, and everything that would
have been sent is inspectable.
It plugs into stapel-notifications through that module's email-provider seam
by dotted path — one setting, no import between modules, nothing to undo
before production beyond flipping the setting back.
Quick start
# settings.py
INSTALLED_APPS = [
# ...
"stapel_mailtrap",
]
# urls.py
path("mailtrap/", include("stapel_mailtrap.urls"))
python manage.py migrate
Trapping notifications email
Point stapel-notifications' email provider at the trap — one setting, no code:
STAPEL_NOTIFICATIONS = {
"EMAIL_PROVIDER": "stapel_mailtrap.provider.MailtrapEmailProvider",
}
Every email stapel-notifications would send is now persisted and inspectable
instead. The provider matches the notifications email duck type
(send(recipient, subject, html_body, headers)) — see MODULE.md §3.
Trapping any backend
For a host whose own mail path should trap, call the service directly:
from stapel_mailtrap import trap_email
trap_email(
to_email="alice@example.com",
from_email="bot@example.com",
subject="Your code",
body_html="<b>123456</b>",
body_text="123456",
attachments=[{"filename": "invoice.pdf", "content_type": "application/pdf", "size": 8123}],
scope_key="workspace-42", # opaque host scope
)
It persists the row and emits mailtrap.email.trapped transactionally.
Attachments are metadata only — raw bytes are never stored.
HTTP API
Read-only, by design: the trap is filled by the provider or by trap_email,
never through the API.
| Route | Meaning |
|---|---|
GET mailtrap/emails/ |
Trapped emails, newest first, paginated (anchor); ?scope_key= filters |
GET mailtrap/emails/<uuid>/ |
One trapped email — full HTML/text bodies + attachment metadata |
The default permission is staff/service only, and that default is deliberate:
trapped mail contains OTPs and magic links, so a wide-open journal is a
credential store with a browsable UI. Override permission_classes and set a
SCOPE_PROVIDER for per-tenant visibility — see MODULE.md.
Retention
A trap fills up and nothing ever asks it to stop, so both caps are on by default:
STAPEL_MAILTRAP = {
"MAX_EMAILS": 1000, # hard cap on stored rows (0 disables)
"TTL_DAYS": 30, # delete rows older than N days (0 disables)
}
Run the sweep from any scheduler:
python manage.py purge_trapped_emails # --dry-run supported
Or schedule the Celery task (celery is an optional host dependency):
CELERY_BEAT_SCHEDULE = {
"purge-trapped-emails": {
"task": "stapel_mailtrap.tasks.purge_trapped_emails",
"schedule": crontab(hour=3, minute=0), # nightly
},
}
Settings
All configuration lives in the STAPEL_MAILTRAP namespace (dict setting, flat
setting, or env var — resolved lazily):
| Key | Default | Meaning |
|---|---|---|
SCOPE_PROVIDER |
"stapel_mailtrap.scope.DefaultScopeProvider" |
Dotted path to a ScopeProvider subclass — the multi-tenant seam. |
MAX_EMAILS |
1000 |
Retention count cap (0 disables). |
TTL_DAYS |
30 |
Retention age cap in days (0 disables). |
STORE_BODY |
True |
False stores the envelope only (bodies empty). |
Bus events
| Kind | Name | Contract |
|---|---|---|
| Event | mailtrap.email.trapped |
{email_id, to, from?, subject, scope_key, attachment_count, trapped_at} — schema |
Extension points
See MODULE.md
— the agent-facing map of every fork-free seam (settings, the ScopeProvider
seam, the notifications email-provider adapter with its duck-typed contract,
serializer seams, permissions, comm surface, system checks).
Development
pip install -e . && pip install pytest pytest-django pytest-cov ruff jsonschema
./setup-hooks.sh
pytest tests/
License
MIT — see LICENSE.
This page is assembled by stapel-readme from docs/readme.md plus the contract artifacts in docs/. Edit the prose in docs/readme.md; the badges, facts and links above and below it are generated — do not hand-edit README.md.
Metadata
Release files for stapel-mailtrap 0.2.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 |
|---|---|---|---|---|
| stapel_mailtrap-0.2.0.tar.gz | 38.7 kB | With provenance | Details for stapel_mailtrap-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| stapel_mailtrap-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for stapel_mailtrap-0.2.0-py3-none-any.whl |
Total release size: 79.1 kB
Release files / stapel_mailtrap-0.2.0.tar.gz
| Download URL | stapel_mailtrap-0.2.0.tar.gz |
|---|---|
| Size | 38.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a3fb5d276f5b3903c9b99f2489579b87647ab8baa10e4c24ed623f1acd19cc7c
|
|
BLAKE2b-256 checksum How to use checksums |
7fda8f2b223974c1fc84765ec45cd0b44dc6caf834ac53e2b9365698f717e080
|
| 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 20, 2026.
Transparency logRelease files / stapel_mailtrap-0.2.0-py3-none-any.whl
| Download URL | stapel_mailtrap-0.2.0-py3-none-any.whl |
|---|---|
| Size | 40.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3f58362d8f2540b0fba25ba87c75806a38b5b91142bb5fa5426156a0c379f1b1
|
|
BLAKE2b-256 checksum How to use checksums |
97d0e3a00225db70ac3eeb8bc34415d3ddef46585afe0831c9385fe6722905bd
|
| 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 20, 2026.
Transparency logSecurity for stapel-mailtrap 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.
Attestation for stapel_mailtrap-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 20, 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.