# Better Auth email with Groundmail

Better Auth has a large [plugin catalog](https://better-auth.com/docs/plugins) (Magic Link, Email OTP, Organization, Stripe, etc.).

What it does **not** have is a third-party “email provider” plugin. Email delivery is always **your callback**:

| Surface | Callback you supply |
|---------|---------------------|
| Core email verification | `emailVerification.sendVerificationEmail` |
| Core password reset | `emailAndPassword.sendResetPassword` |
| [Magic Link plugin](https://www.better-auth.com/docs/plugins/magic-link) | `magicLink({ sendMagicLink })` |
| [Email OTP plugin](https://better-auth.com/docs/plugins/email-otp) | `emailOTP({ sendVerificationOTP })` |
| Organization invites | `organization({ sendInvitationEmail })` |

Groundmail’s integration is `createBetterAuthEmail` from [`groundmail-better-auth`](https://www.npmjs.com/package/groundmail-better-auth): one helper that returns those callbacks, wired to `POST /emails`.

Better Auth’s own hosted mail (`@better-auth/infra`) is a separate product. This guide is for bringing **your** Groundmail account.

**Using Neon Auth?** Neon runs Better Auth for you (Managed Better Auth), so there's no `betterAuth()` config to pass these callbacks to. Use custom SMTP or Neon's `send.otp` and `send.magic_link` webhooks instead: see [Neon Auth email](https://docs.groundmail.dev/integrations/neon).

---

## Prerequisites

1. Groundmail account + `gm_…` API key
2. Verified sending domain (or Sandbox for self-tests — see [Quickstart](https://docs.groundmail.dev/quickstart))
3. Better Auth app

```bash
npm install groundmail-better-auth groundmail better-auth
```

```bash
GROUNDMAIL_API_KEY=gm_your_key_here
```

Public package: [`groundmail-better-auth`](https://www.npmjs.com/package/groundmail-better-auth) (also available as `groundmail/better-auth` from the core SDK).

---

## Full setup (core + plugins)

```ts
// lib/auth.ts
import { betterAuth } from 'better-auth'
import { magicLink, emailOTP, organization } from 'better-auth/plugins'
import { createBetterAuthEmail } from 'groundmail-better-auth'

const email = createBetterAuthEmail({
  from: 'noreply@yourdomain.com',
  apiKey: process.env.GROUNDMAIL_API_KEY!,
  appUrl: process.env.BETTER_AUTH_URL, // used for invite links
})

export const auth = betterAuth({
  emailVerification: {
    sendVerificationEmail: email.sendVerificationEmail,
  },
  emailAndPassword: {
    enabled: true,
    sendResetPassword: email.sendResetPassword,
  },
  plugins: [
    magicLink({
      sendMagicLink: email.sendMagicLink,
    }),
    emailOTP({
      sendVerificationOTP: email.sendVerificationOTP,
    }),
    organization({
      sendInvitationEmail: email.sendInvitationEmail,
    }),
  ],
})
```

Only enable the plugins you use. Core callbacks work without any plugins.

---

## What each callback sends

| Helper | Tag | Content |
|--------|-----|---------|
| `sendVerificationEmail` | `auth=verify` | Verification link |
| `sendResetPassword` | `auth=reset` | Reset link |
| `sendMagicLink` | `auth=magic-link` | Sign-in link |
| `sendVerificationOTP` | `auth=otp-{type}` | One-time code (`sign-in`, `email-verification`, `forget-password`) |
| `sendInvitationEmail` | `auth=invite` | Org invite link (`{appUrl}/accept-invitation/{id}`) |

Customize templates with `email.sendAuthEmail({ to, subject, html, text, tag })` or `email.sendLinkEmail(...)`.

---

## Options

| Option | Default | Purpose |
|--------|---------|---------|
| `from` | required | Verified sender |
| `apiKey` | `process.env.GROUNDMAIL_API_KEY` | API key |
| `baseUrl` | `https://api.groundmail.dev` | API base URL |
| `appUrl` | — | Base URL for organization invite links |
| `fireAndForget` | `true` | Don’t await send in the auth request (Better Auth timing guidance). Set `false` in tests. |
| `client` | — | Inject a `Groundmail` instance |

---

## Related

- [Better Auth plugins](https://better-auth.com/docs/plugins)
- [Auth providers](https://docs.groundmail.dev/guides/password-reset-emails#auth-providers)
- [Node.js SDK](https://docs.groundmail.dev/frameworks/node)
- [Password reset emails](https://docs.groundmail.dev/guides/password-reset-emails)
