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

# Email

> Contact sync and auth email delivery with Loops, Resend, and Brevo

<Info>
  This page covers both **email contact sync** (building your mailing list) and **auth emails** (magic links, password resets). One provider handles both jobs.
</Info>

## What Email Does in This Repo

The email service handles two things through a single provider:

1. **Contact sync** -- when a user signs in, they're created as a contact in your email tool
2. **Auth emails** -- magic link and password reset emails are delivered through the same provider

This means you can build a mailing list, sync user data, and handle auth emails all from one place.

<Note>
  If contact sync fails, sign-in still works normally. Auth email delivery is stricter because the user needs that email to finish the flow.
</Note>

## Pick Your Provider

<Tabs>
  <Tab title="Loops">
    **Best for:** Simple defaults, easy contact syncing, and templated auth emails.

    Loops uses transactional template IDs for auth emails, so you'll create the email templates in Loops and reference them by ID.

    ```env theme={null}
    NEXT_PUBLIC_EMAIL_PROVIDER=loops
    LOOPS_API_KEY=your-api-key
    AUTH_EMAIL_FROM="YourApp <auth@example.com>"
    LOOPS_AUTH_MAGIC_LINK_TRANSACTIONAL_ID=your-template-id
    LOOPS_AUTH_RESET_PASSWORD_TRANSACTIONAL_ID=your-template-id
    ```

    <Warning>
      Don't forget the transactional template IDs. Without them, Loops won't know which email to send for magic links and password resets.
    </Warning>
  </Tab>

  <Tab title="Resend">
    **Best for:** Developer-friendly API, simple contact management, and app-managed auth emails.

    Resend sends auth emails directly from your app -- no template setup needed on their side.

    ```env theme={null}
    NEXT_PUBLIC_EMAIL_PROVIDER=resend
    RESEND_API_KEY=your-api-key
    RESEND_SEGMENT_ID=your-segment-id
    AUTH_EMAIL_FROM="YourApp <auth@example.com>"
    ```

    <Tip>
      `RESEND_SEGMENT_ID` is optional. Use it only if you want contacts automatically added to a specific segment.
    </Tip>
  </Tab>

  <Tab title="Brevo">
    **Best for:** Contact lists, CRM-like contact management, and custom purchase-state attributes.

    Brevo also sends auth emails directly from your app.

    ```env theme={null}
    NEXT_PUBLIC_EMAIL_PROVIDER=brevo
    BREVO_API_KEY=your-api-key
    BREVO_LIST_ID=2
    BREVO_PURCHASED_ATTRIBUTE=PURCHASED
    AUTH_EMAIL_FROM="YourApp <auth@example.com>"
    ```

    <Tip>
      Use `BREVO_PURCHASED_ATTRIBUTE` only if that attribute already exists in your Brevo account.
    </Tip>
  </Tab>
</Tabs>

## Provider Selection

Set the provider with:

```env theme={null}
NEXT_PUBLIC_EMAIL_PROVIDER=loops
```

You can optionally add a server-only override that takes priority:

```env theme={null}
EMAIL_PROVIDER=loops
```

Supported values: `loops`, `resend`, `brevo`, `none`

## Setup

<Steps>
  <Step title="Pick a provider">
    Choose Loops, Resend, or Brevo based on your needs.
  </Step>

  <Step title="Create an account and get your API key">
    Sign up with your chosen provider and generate an API key.
  </Step>

  <Step title="Add env vars to .env.local">
    Add the provider-specific env vars from the tabs above.
  </Step>

  <Step title="Set up templates (Loops only)">
    If you're using Loops, create transactional email templates for magic links and password resets, then add their IDs to your env vars.
  </Step>

  <Step title="Restart and test">
    Restart the app, sign in with a test account, and request a magic link or password reset email. Confirm the contact appears in your provider's dashboard.
  </Step>
</Steps>

## Key Files

* `lib/email/contacts.ts` -- contact sync logic
* `lib/email/providers/*` -- provider-specific implementations
* `lib/email/auth-emails.ts` -- auth email delivery

## Verify It Works

<Check>
  Your email setup is correct if:

  * A test user signs in successfully
  * Auth emails arrive for magic link and forgot password
  * The user appears in your chosen email provider
  * The app still works even if contact sync temporarily fails
</Check>

## Common Mistakes

<AccordionGroup>
  <Accordion title="Wrong provider name in env vars">
    Make sure `NEXT_PUBLIC_EMAIL_PROVIDER` is exactly `loops`, `resend`, or `brevo`. Typos will silently disable email.
  </Accordion>

  <Accordion title="Missing AUTH_EMAIL_FROM">
    Auth emails need a sender address. Without `AUTH_EMAIL_FROM`, delivery will fail.
  </Accordion>

  <Accordion title="Using Loops without template IDs">
    Loops requires `LOOPS_AUTH_MAGIC_LINK_TRANSACTIONAL_ID` and `LOOPS_AUTH_RESET_PASSWORD_TRANSACTIONAL_ID`. Without them, auth emails won't send.
  </Accordion>

  <Accordion title="Segment/list/attribute ID doesn't exist">
    If you reference a segment, list, or attribute that doesn't exist in your provider, contact sync will fail silently.
  </Accordion>

  <Accordion title="Expecting auth emails with provider set to 'none'">
    `EMAIL_PROVIDER=none` disables everything. No contact sync, no auth emails.
  </Accordion>
</AccordionGroup>
