# Mail opt-outs

Mail sent through an integration operation that supports opt-outs (today
integrations.ses.emails.send and integrations.ses.emails.send_bulk) must choose
one of two kinds:

- `"transactional": true` for mail the recipient cannot unsubscribe from:
  receipts, invoices, logins, password resets. Nothing changes for these sends.
- `"list": "<name>"` for marketing mail. The name is a lower-case slug such as
  `newsletter`; a list exists as soon as a send or an opt-out uses it.

## What Adlass does for a send with list

- It refuses or skips recipients who unsubscribed from that list or from all
  lists of the Space (list `*`).
- It creates a personal link per recipient
  (https://adlassmail.io/u/<token>/) and adds the headers List-Unsubscribe and
  List-Unsubscribe-Post (RFC 8058 one-click), so Gmail, Yahoo and Outlook show
  their unsubscribe button. Do not set these headers yourself.
- It fills the visible link: `{{unsubscribe_url}}` in text and html is replaced
  (it must appear in every body you send), and with a stored template the
  template data gets `unsubscribe_url`. The template itself must contain
  `{{unsubscribe_url}}`; Adlass cannot check stored templates.

Opt-outs belong to a Space, so a send with list needs a Space context: call it
with space_id and grant_id (routines and workflows run in one). emails.send with
list takes exactly one recipient in `to`; use emails.send_bulk for many.
A refused send answers `integration_invalid_input` with the reason in the
message; `details.emails` lists unsubscribed recipients. send_bulk skips
unsubscribed entries and refuses only when all are; every result carries `to`,
so a recipient without result was skipped.

## The unsubscribe page

The link opens a page on adlassmail.io that shows the sender domain and the
list. One click unsubscribes from the list, a second button from every
marketing mail of the Space. The mail client's one-click button unsubscribes
from the list without a page. No login, no cookies.

## Opt-out list

- mail.opt_outs.list: opt-outs of a Space, newest first, filter by list or email.
- mail.opt_outs.add: import addresses, e.g. unsubscribes from Klaviyo or a shop.
- mail.opt_outs.remove: only when the recipient asked to get the list again.

Every new opt-out emits the event mail.unsubscribed with list, email and source
(`one_click`, `page` or `manual`). A routine on this event can pass unsubscribes on,
for example to the email marketing consent of a Shopify customer: map the list
to the shop, find the customer by email, and skip events your own sync caused.
Removing an opt-out emits no event.

Bounces and complaints are not on this list: the SES account suppression list
handles them (integrations.ses.suppression.*).
