> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ana.fi/llms.txt
> Use this file to discover all available pages before exploring further.

# Notification email

> Verify the inbox of an account with a one-time code, then let rules send email with the notify action.

After this page, you know how an owner verifies the notification inbox of an account, each outcome of the two calls, and how a rule document then uses `notify`.

> [!WARNING]
>
> Today, the engine accepts the sign-in of the code request only when the EIP-4361 message names the domain of the Anafi dapp, `app.ana.fi`. The Anafi API refuses a sign-in for another domain with `422`. An integration on another domain cannot complete this flow today. An owner can verify the inbox in the Anafi dapp. Your app can then name that inbox in `global.notification_email`.

## How it fits together

1. The owner verifies one inbox for the account: a code request, then a code check.
2. The rule document names that inbox in `global.notification_email`.
3. Each `notify` action of the document sends its `message` to that inbox when its rule fires.

The engine refuses at the stage a document whose `global.notification_email` is not the verified inbox of the account (`NotificationEmailNotVerifiedError`). The refusal is the same when the account has another verified inbox, and when it has none.

## Preconditions

* The signer is a signer of the account: an owner of the Safe, or the ERC20Allow address. The account does not need a commit yet.
* `adapter` names an adapter that the deployment serves. The engine checks the signer through the account kind of that adapter.
* The call can reach the Anafi API. Today, the Anafi API hosts answer CORS only for `https://app.ana.fi`.

## Sequence

```mermaid theme={null}
sequenceDiagram
    autonumber
    participant W as Owner wallet
    participant A as Your app (SDK)
    participant API as Anafi API
    participant E as Anafi engine (black box)
    participant C as Chain
    A->>A: Canonical email, EIP-4361 text with a resource
    W-->>A: Sign-in message signature (personal_sign)
    A->>C: AnafiRegistry.engineEncKey()
    A->>API: POST /api/wallets/{account}/notification_email/request_code (sealed)
    API->>E: Relay
    E-->>A: Sealed reply: outcome, code_token
    Note over E: Sends an email with a 6-digit code to the inbox
    A->>API: POST /api/wallets/{account}/notification_email/verify_code (sealed)
    API->>E: Relay
    E-->>A: Sealed reply: verified, wrong_code, or code_token_invalid
```

## Steps

**1. Request a code**

Call `requestNotificationEmailCode({ aa, adapter, email, eoa })`.

1. The SDK computes the canonical form of `email`: it removes ASCII whitespace at the two ends and changes it to lower case. It refuses an empty address, a non-ASCII character, inner whitespace, and the characters `<`, `>`, `,`, `;`, `"`. Use `canonicalNotificationEmail(raw)` to run the same check in your form; it returns `null` for a refused address.
2. It builds an EIP-4361 message: the statement `Verify <email> to receive notifications from wallet <account>.`, an `Expiration Time` 5 minutes after `Issued At`, and one resource `urn:anafi:notification-email-code:<account>:<keccak256(email)>`. The wallet shows a sign-in message prompt.
3. It encrypts `{ adapter, email, siwe_message, signature }` to the engine, bound to the route, the network and the account. It sends `POST /api/wallets/{aa}/notification_email/request_code` with `{ "sealed": ... }`.
4. It opens the reply and checks that it names the same account.

The result is `{ aa, email, outcome }`:

| `outcome.kind` | Meaning | What to do |
| - | - | - |
| `code_issued` | The engine made a code. `outcome.code_token` is a 16-byte hex token. `outcome.delivery` is `sent` or `failed`. | Keep `code_token`. With `failed`, the mail server did not take the email: request again later. |
| `already_verified` | This inbox is already the verified inbox of the account. | Go to the rule document step. |
| `invalid_email` | The engine does not accept this address. `email` is `null`. | Ask for another address. |
| `retry_after` | Too many code emails for now. `outcome.seconds` says how long to wait. | Wait that many seconds. |
| `mail_not_configured` | This deployment sends no email. | Notifications are not available on this network. |

**2. Check the code**

The owner reads the 6-digit code in the email. The code expires 15 minutes after it was sent.

Call `verifyNotificationEmailCode({ aa, codeToken, code })`. There is no wallet prompt. The SDK encrypts `{ code_token, code }` and sends `POST /api/wallets/{aa}/notification_email/verify_code`.

| `outcome.kind` | Meaning | What to do |
| - | - | - |
| `verified` | The inbox in `email` is now the verified inbox of the account. | Go to the rule document step. |
| `wrong_code` | The code is wrong. `outcome.attempts_left` says how many tries are left. | Ask again. At 0, request a new code. |
| `code_token_invalid` | The token is unknown, expired, or has no tries left. `email` is `null`. | Request a new code. |

**3. Name the inbox in the document**

Set `global.notification_email` to the canonical inbox, and add `notify` actions:

```yaml theme={null}
global:
  notification_email: "owner@example.com"
  fees_method: { type: fixed_token, token: USDC }
rules:
  rent:
    trigger:
      type: calendar
      day_of_month: [1]
      time_of_day: "09:00"
      timezone: "Europe/Berlin"
    actions:
      - type: send_one
        to: "0x1111111111111111111111111111111111111111"
        value: "500 USDC"
      - type: notify
        channel: email
        message: "Rent paid."
```

`documentWithNotificationEmail(yaml, canonicalEmail)` sets the field for you. When the document already names the same inbox, it keeps the text of that document. `documentNotificationEmail(yaml)` reads the field. Then stage and commit as usual.

## What `notify` does

| Fact | Detail |
| - | - |
| Destination | Every `notify` of a document goes to `global.notification_email`. |
| Channel | `channel` is accepted (`email`, `push`, `telegram`), and the engine sends every `notify` by email. |
| Message | `message` is sent verbatim. `${...}` text is not filled in. |
| No inbox | A `notify` in a document with no `global.notification_email` delivers nothing. The stage returns a finding about it. |
| Funds | A `notify` never moves funds. |

See [Actions](https://docs.ana.fi/rules/actions.md) and [Document](https://docs.ana.fi/rules/document.md).

## What can refuse

| Error | HTTP | Meaning |
| - | - | - |
| `Error` from the SDK | none | The address is not a bare ASCII address, or the reply names another account. |
| `BackendRequestError` | `422` | The request did not open, the sign-in did not verify (domain, chain, time window, resource, replay), the signer is not a signer of the account, or the adapter is not served. |
| `BackendRequestError` | `502` | The engine could not read the authorization state, or did not answer. |
| `NotificationEmailNotVerifiedError` | `422` at the stage | The document names an inbox that is not the verified inbox. |

## Resume

| State | What to do |
| - | - |
| Page reload after `code_issued` | Keep `code_token` in your session. Call `verifyNotificationEmailCode` with it while the code is valid. |
| Lost `code_token` | Request a new code. Watch for `retry_after`. |
| Inbox verified, commit not done | `recover` returns `verified_notification_email`. Stage the document with that inbox. |

## Example

```ts theme={null}
import {
  canonicalNotificationEmail,
  documentWithNotificationEmail,
  type AnafiClient,
  type Hex,
} from "@anafi-cc/anafi-sdk";

export async function verifyInbox(
  anafi: AnafiClient,
  aa: Hex,
  eoa: Hex,
  adapter: Hex,
  rawEmail: string,
  askForCode: () => Promise<string>,
) {
  const email = canonicalNotificationEmail(rawEmail);
  if (email === null) throw new Error("enter a plain email address");

  const request = await anafi.requestNotificationEmailCode({ aa, adapter, email, eoa });
  switch (request.outcome.kind) {
    case "already_verified":
      return email;
    case "retry_after":
      throw new Error(`wait ${request.outcome.seconds} seconds`);
    case "invalid_email":
    case "mail_not_configured":
      throw new Error(request.outcome.kind);
    case "code_issued": {
      for (;;) {
        const check = await anafi.verifyNotificationEmailCode({
          aa,
          codeToken: request.outcome.code_token,
          code: await askForCode(),
        });
        if (check.outcome.kind === "verified") return email;
        if (check.outcome.kind === "code_token_invalid" || check.outcome.attempts_left === 0) {
          throw new Error("request a new code");
        }
      }
    }
  }
}

export function withInbox(yaml: string, email: string): string {
  return documentWithNotificationEmail(yaml, email);
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.