# stapel-mailtrap 0.2.1

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.

Contract: axes 2 · surface 3 · extension points 3 · operations 2 · error codes 44.
Generated from docs/capabilities.json by `stapel-llms-txt` — do not edit; drift-gated by `make contract-check`.

## Configuration axes — what a product switches on
Settings keys; `default` is what you get by saying nothing. Turning an axis off unmounts the operations it gates.
- MAX_EMAILS [enum, default 1000] — Maximum trapped emails retained
  Hard cap on stored messages; the retention sweep deletes the oldest rows beyond this many. 0 disables the count cap (conf.py).
- STORE_BODY [bool, default true] — Store full email bodies (vs. envelope only)
  Store the rendered HTML/text bodies on the row. A host that only needs the envelope (to/from/subject) for a lighter footprint can turn this off; bodies are then stored empty (conf.py, MODULE.md Settings table).

## Usage surface — call these before writing your own
This is the answer to "does Stapel already have something for X?". `instead of` names the outside symbol this one displaces.
### gate_function
- purge_expired — stapel_mailtrap.services.purge_expired
  instead of: a hand-written TrappedEmail.objects.filter(...).delete() sweep
  The retention sweep (STAPEL_MAILTRAP['TTL_DAYS'] + ['MAX_EMAILS']) shared by the purge_trapped_emails management command and the Celery task. Call this directly when a host needs the sweep on its own schedule or trigger (an admin 'purge now' button, a different task queue) — it is the one place both limits are implemented, so a hand-written deletion query is one of the two casually forgotten.
- trap_email — stapel_mailtrap.services.trap_email
  instead of: a second hand-rolled mail-capture table, logging outbound email ad hoc
  The single write path for capturing one outbound email as a TrappedEmail row, transactionally emitting mailtrap.email.trapped. Reach for this directly from a host's OWN mail-sending code path that does not go through stapel-notifications' EMAIL_PROVIDER seam (which already routes through this function via MailtrapEmailProvider) — do not hand-roll a second capture table or a bare logger.info() of outbound mail in dev/staging.
### factory
- get_scope_provider — stapel_mailtrap.scope.get_scope_provider
  instead of: re-reading STAPEL_MAILTRAP['SCOPE_PROVIDER'] and instantiating it by hand
  Resolve the configured STAPEL_MAILTRAP['SCOPE_PROVIDER'] instance — the same call TrappedEmailListView/TrappedEmailDetailView make before filtering. Reach for this from any host-written view or report over TrappedEmail so it stays subject to the same tenant scope as the built-in Mail API, instead of re-reading the setting and instantiating the provider by hand (which is exactly how one code path could end up unscoped while the others are not).

## Extension points — what a product replaces, fork-free
- SCOPE_PROVIDER [dotted_path]
  REPLACE, single strategy: a ScopeProvider subclass (filter(queryset, request)). Default is a no-op single global scope; a multi-tenant host filters so one tenant never reads another's trapped mail (scope.py, MODULE.md Extension point 2).
- notifications email provider [duck_typed_dotted_path]
  MailtrapEmailProvider implements notifications' duck-typed send(recipient, subject, html_body, headers) contract; wired via STAPEL_NOTIFICATIONS['EMAIL_PROVIDER'] = 'stapel_mailtrap.provider.MailtrapEmailProvider', no import either direction (provider.py, MODULE.md Extension point 3).
- serializer_seams [class_override]
  Both views carry response_serializer_class via SerializerSeamMixin (TrappedEmailListItemSerializer / TrappedEmailDetailSerializer); swap by subclassing and remounting (views.py:30-33, MODULE.md Extension point 4).

## Fits with — fleet dependencies
- stapel-core (required) — the only stapel-* dependency: AppSettings config layer (pyproject.toml dependency)
- stapel-notifications (optional) — consumer of this module's email-provider seam via a duck-typed dotted path (STAPEL_NOTIFICATIONS['EMAIL_PROVIDER']); no direct import either direction (MODULE.md Extension point 3)

## HTTP operations (2) — call by operationId, never by a typed path
Paths are relative to `/mailtrap/api/v1/emails/`.
### Mail
- GET /{email_id}/ — get_trapped_email
- GET / — list_trapped_emails

## Error codes (44) — the StapelError envelope
Render `t(code, params)`; branch UX on the remediation. Localized text lives in docs/errors.<lang>.md, not here.
- error.400.bad_request [400] fix_input
- error.400.captcha_invalid [400] retry
- error.400.captcha_required [400] retry
- error.400.expected_list [400] fix_input
- error.400.field.blank [400] fix_input {field}
- error.400.field.does_not_exist [400] fix_input {field}
- error.400.field.invalid [400] fix_input {field}
- error.400.field.invalid_choice [400] fix_input {field}
- error.400.field.max_length [400] fix_input {field,max_length}
- error.400.field.max_value [400] fix_input {field,max_value}
- error.400.field.min_length [400] fix_input {field,min_length}
- error.400.field.min_value [400] fix_input {field,min_value}
- error.400.field.null [400] fix_input {field}
- error.400.field.required [400] fix_input {field}
- error.400.field.unique [400] fix_input {field}
- error.400.invalid_ad_id [400] fix_input
- error.400.too_many_ids [400] fix_input {requested,limit}
- error.400.validation_error [400] fix_input
- error.400.verification_failed [400] verify
- error.400.verification_invalid_factor [400] verify
- error.401.unauthorized [401] reauthenticate
- error.402.payment_required [402] retry
- error.403.forbidden [403] retry
- error.403.network_blocked [403] contact_support
- error.403.verification_enrollment_required [403] verify
- error.403.verification_required [403] verify
- error.404.ad_not_found [404] retry
- error.404.mailtrap_email_not_found [404] retry
- error.404.not_found [404] retry
- error.404.verification_challenge_not_found [404] verify
- error.405.method_not_allowed [405] retry
- error.406.not_acceptable [406] retry
- error.408.request_timeout [408] retry
- error.409.conflict [409] fix_input
- error.410.gone [410] retry
- error.413.payload_too_large [413] retry
- error.415.unsupported_media_type [415] retry
- error.422.unprocessable_entity [422] wait_and_retry
- error.423.locked [423] wait_and_retry
- error.423.verification_locked [423] wait_and_retry
- error.429.rate_limit [429] wait_and_retry {retry_after_display,retry_after,retry_after_minutes}
- error.429.too_many_requests [429] wait_and_retry
- error.500.internal [500] contact_support
- error.503.mandate_unavailable [503] retry
