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

# Test emails with AgentMail

> Give each test its own AgentMail inbox and read the emails your app sends to it.

You can use the [AgentMail SDK](https://docs.agentmail.to/quickstart) in your
tests to create an inbox for each test and read the emails your app sends to
it. Because your app sends real email through its configured provider, you
test delivery alongside the email content.

In this guide, we create a temporary inbox for a test and check for the email
using the AgentMail SDK.

## Connect your account

Before we run the tests, we need an API key from the
[AgentMail console](https://console.agentmail.to) set as the
`AGENTMAIL_API_KEY` environment variable.

## Installation

To install the SDK, run:

<CodeGroup>
```bash npm
npm install --save-dev agentmail
```

```bash pnpm
pnpm add -D agentmail
```

```bash bun
bun add -d agentmail
```

</CodeGroup>

## Configure e2e

Next, we register the API key with `registerProviderKey` so e2e redacts it
from reports, traces, and model input:

```ts title="e2e.config.ts"

registerProviderKey('agentmail', process.env.AGENTMAIL_API_KEY);

export default defineConfig({
  projectId: 'dev.example.signup',
  targets: [
    {
      name: 'web',
      engine: web(),
      app: {
        url: 'http://127.0.0.1:0',
        command: {
          executable: 'node',
          args: ['app/server.mjs'],
          env: { PORT: '{port}' },
        },
      },
    },
  ],
});
```

Our app still sends the email through its own provider. If it reads that
provider's key from the environment, we pass the key to it with
[`inheritEnv`](https://e2e.com/docs/reference/config#command) on `app.command` and register it with `registerProviderKey` too.

## Create an inbox and read the email

At the top of our test file, we add a `createInbox()` helper that creates
and cleans up the inbox.

Once we have the inbox, we sign up with its address, wait for the email with
[`toPass()`](https://e2e.com/docs/reference/expect#retrying-a-block), and read it with
`messages.get()`:

```ts title="tests/signup.e2e.ts"

const agentmail = new AgentMailClient();

async function createInbox() {
  const inbox = await agentmail.inboxes.create({ clientId: randomUUID() });

  return {
    ...inbox,
    [Symbol.asyncDispose]: () => agentmail.inboxes.delete(inbox.inboxId),
  };
}

test('signing up sends a welcome email', async ({ app, screen }) => {
  await using inbox = await createInbox();

  await app.open('/');
  await screen.getByRole('textbox', { name: 'Email' }).fill(inbox.email);
  await screen.getByRole('button', { name: 'Sign up' }).click();

  let id = '';

  await expect(async () => {
    const { messages } = await agentmail.inboxes.messages.list(inbox.inboxId);

    expect(messages).toHaveLength(1);
    id = messages[0]?.messageId ?? '';
  }).toPass({ timeout: 60_000 });

  const email = await agentmail.inboxes.messages.get(inbox.inboxId, id);

  expect(email.subject).toMatch(/welcome/i);
  expect(email.html).toContain('Confirm your email');
});
```

Because we created a fresh inbox for this test, the only email in it is the
one our app sent.

We give `toPass()` a timeout below the test timeout. If the email never
arrives, the test fails on the assertion and still deletes the inbox. A test
that hits the test timeout is stopped without waiting for it, so the delete
might not finish. The `clientId` lets AgentMail return the same inbox when the
SDK retries a failed create, so a retry doesn't leave a second inbox behind.

## Limits

- There's a cap on how many inboxes we can have at the same time, depending
  on our plan. On the free plan, we get 3, and any existing inboxes count
  toward that limit. Deleting an inbox frees up a slot. We need to keep
  `workers` at or below the number of open slots, or `inboxes.create()` fails
  with a 403 error.
- Daily and monthly email limits depend on our plan. We can check the current
  numbers on [AgentMail pricing](https://www.agentmail.to/pricing).
- Because our app sends real email, every test counts toward our email
  provider's quota.

## Next

<CardGroup cols={2}>
  <Card title="Writing fixtures" href="/fixtures">
    How to share the inbox setup across tests with a fixture.
  </Card>
  <Card title="Retrying a block" href="/reference/expect#retrying-a-block">
    How `toPass()` retries assertions until they pass or the test times out.
  </Card>
</CardGroup>
