Metadata-Version: 2.4
Name: stapel-mailtrap
Version: 0.2.1
Summary: Email trap (mail catcher) for the Stapel framework — capture outbound email in dev/staging
License: MIT
Project-URL: Homepage, https://github.com/usestapel/stapel-mailtrap
Project-URL: Repository, https://github.com/usestapel/stapel-mailtrap
Project-URL: Documentation, https://github.com/usestapel/stapel-mailtrap#readme
Project-URL: Changelog, https://github.com/usestapel/stapel-mailtrap/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/usestapel/stapel-mailtrap/issues
Keywords: django,stapel,email,mailtrap,mailcatcher
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: stapel-core<1.0,>=0.99.3
Provides-Extra: all
Dynamic: license-file

<!-- Generated by stapel-readme from docs/readme.md + docs/*.json. Do not edit this file; edit docs/readme.md and re-run `make readme`. -->

# stapel-mailtrap

[![CI](https://img.shields.io/github/actions/workflow/status/usestapel/stapel-mailtrap/ci.yml?branch=main&logo=github&label=CI)](https://github.com/usestapel/stapel-mailtrap/actions/workflows/ci.yml?query=branch%3Amain)
[![coverage](https://img.shields.io/codecov/c/github/usestapel/stapel-mailtrap?branch=main&logo=codecov&label=coverage)](https://app.codecov.io/gh/usestapel/stapel-mailtrap)
[![pypi](https://img.shields.io/pypi/v/stapel-mailtrap?logo=pypi&logoColor=white&label=pypi)](https://pypi.org/project/stapel-mailtrap/)
[![downloads](https://static.pepy.tech/badge/stapel-mailtrap/month)](https://pepy.tech/project/stapel-mailtrap)
[![python](https://img.shields.io/pypi/pyversions/stapel-mailtrap?logo=python&logoColor=white)](https://pypi.org/project/stapel-mailtrap/)
[![license](https://img.shields.io/github/license/usestapel/stapel-mailtrap)](https://github.com/usestapel/stapel-mailtrap/blob/main/LICENSE)
[![llms.txt](https://img.shields.io/badge/llms.txt-blue)](https://github.com/usestapel/stapel-mailtrap/blob/main/docs/llms.txt)

> 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](https://github.com/usestapel) — composable Django apps that deploy as a monolith or as microservices without changing module code.

## Install

```bash
pip install stapel-mailtrap
```

## At a glance

| Fact | Value |
|---|---|
| Version | `0.2.1` |
| 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`](https://github.com/usestapel/stapel-core) · [`stapel-notifications`](https://github.com/usestapel/stapel-notifications) (optional) |

## Documentation

[OpenAPI](https://github.com/usestapel/stapel-mailtrap/blob/main/docs/schema.json) · [capabilities.json](https://github.com/usestapel/stapel-mailtrap/blob/main/docs/capabilities.json) · [llms.txt (for agents)](https://github.com/usestapel/stapel-mailtrap/blob/main/docs/llms.txt)

## 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

```python
# settings.py
INSTALLED_APPS = [
    # ...
    "stapel_mailtrap",
]

# urls.py
path("mailtrap/", include("stapel_mailtrap.urls"))
```

```bash
python manage.py migrate
```

## Trapping notifications email

Point `stapel-notifications`' email provider at the trap — one setting, no code:

```python
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:

```python
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:

```python
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:

```bash
python manage.py purge_trapped_emails            # --dry-run supported
```

Or schedule the Celery task (celery is an optional host dependency):

```python
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](https://github.com/usestapel/stapel-mailtrap/blob/main/schemas/emits/mailtrap.email.trapped.json) |

## Extension points

See [MODULE.md](https://github.com/usestapel/stapel-mailtrap/blob/main/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

```bash
pip install -e . && pip install pytest pytest-django pytest-cov ruff jsonschema
./setup-hooks.sh
pytest tests/
```

## License

MIT — see [LICENSE](https://github.com/usestapel/stapel-mailtrap/blob/main/LICENSE).

---

<sub>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`.</sub>
