# Use cases and traces

> Each e-mail can say which use case sent it, and what happened to it shows up in that case's trace.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://mail.docs.sypnia.com/events/use-cases/

For agents: Case attribution in Mail: send option case or header Sypnia-Case (send, sendBatch, SMTP relay header Sypnia-Case); inside a request traced by @sypnia/telemetry the SDK reads the active case and sends traceparent, so case can be omitted. Stored on the e-mail (case, trace id); events carry case; mail events --case <id>. A case file declares its e-mails: mail: { sends: [order-ready] } (template names); the Cases check reports a case that sends a template it does not list, or lists one it never sends. Spans: mail.send <template> in the trace of the request (from, template@version, recipients count, result queued/suppressed/rejected, never the body or addresses); later events (delivered, bounced, complained, delivery_delayed) are added to the same trace as span events on that span, with their time; a bounce marks the span as an error. Cases shows per case: e-mails sent, delivered, bounced, complained.

A confirmation, a receipt, «your bread is ready»: every e-mail your app sends exists because of something a person did. Mail records **which use case** sent it, so «did the confirmation of this order arrive?» is one click from the order.

## Saying the case

```ts
await mail.send({
  from: 'La Espiga <orders@laespiga.co>',
  to: 'ana@example.com',
  template: 'order-ready',
  data: { name: 'Ana', order: '1042' },
  case: 'place-order',
});
```

or the header `Sypnia-Case: place-order`, on the API and on the [SMTP relay](https://mail.docs.sypnia.com/sending/smtp-relay). The id is the one in your map, from `cases/<area>/<id>.case.yaml` ([Cases](https://cases.docs.sypnia.com/)).

When your server is traced with Telemetry's SDK, the case of the current request is already known: `@sypnia/mail` reads it and sends the trace along, so you can leave `case` out.

The case is kept with the e-mail and on every one of its [events](https://mail.docs.sypnia.com/events/event-log), so you can search by it:

```bash
npx @sypnia/mail events --case place-order --type bounced --since 7d
```

## The case says which e-mails it sends

`cases/orders/place-order.case.yaml`

```yaml
id: place-order
title: Place an order
actor: customer
mail:
  sends: [order-ready]
```

The Cases check compares that with what really happens: a case that sends a template it does not list, or lists one it never sends, is reported.

## In the trace

The send is a step (a span) inside the trace of its case, next to the database queries and the page that started it. What happens later is added to the same span when Mail learns it, even hours afterwards:

| On the span `mail.send order-ready` | What it says                                                                                     |
| ----------------------------------- | ------------------------------------------------------------------------------------------------ |
| the send                            | the template and its version, how many recipients, whether it was queued, suppressed or rejected |
| `delivered`, `delivery_delayed`     | when the receiving server accepted it, or why it is still trying                                 |
| `bounced`, `complained`             | the kind of bounce or the complaint; the span is marked as an error                              |

No span carries the body or the addresses. With [Sypnia Telemetry](https://telemetry.docs.sypnia.com/), «Place an order sends the confirmation, and this one bounced» is in the same trace as the order, and in [Sypnia Cases](https://cases.docs.sypnia.com/) each case shows how many of its e-mails were delivered, bounced or marked as spam.
