# Send React Email with Groundmail

Use [React Email](https://react.email) to author components, preview them locally, then **send React Email** through the Groundmail Node SDK — without tying delivery to a single ESP. The SDK accepts a `react` element and compiles it to HTML before `POST /emails`.

You do **not** need to fork React Email or add `@react-email/components` to Groundmail — keep authoring in your app. Groundmail is **not affiliated** with the React Email project; we are a transactional send API that optionally renders your components at send time.

## About React Email

[React Email](https://react.email) is a **separate open-source project** (MIT license) for authoring email UIs with React. Install `@react-email/render` and optional `@react-email/components` in **your** application. Groundmail does not bundle React Email — the SDK optionally calls `@react-email/render` at send time to produce HTML for our API. Author and preview with React Email’s own CLI; delivery is through Groundmail.

---

## Prerequisites

- A Groundmail API key (`gm_…`)
- A verified sending domain (or sandbox: `from` = `noreply@mail.groundmail.dev`, `to` = your account email)
- Node 18+

---

## 1. Install

```bash
npm install groundmail @react-email/render react
# Optional — components + local preview tooling from React Email
npm install @react-email/components
npx create-email@latest
```

`@react-email/render` is an **optional peer** of `groundmail`. Install it only if you use the `react` option. Without it, send with `html` / `text` / `template` as usual.

---

## 2. Author and preview

Create a component (example):

```tsx
// emails/WelcomeEmail.tsx
import * as React from 'react'
import { Html, Button, Text } from '@react-email/components'

export function WelcomeEmail({ name }: { name: string }) {
  return (
    <Html>
      <Text>Hi {name}, welcome aboard.</Text>
      <Button href="https://yourapp.com/dashboard">Open dashboard</Button>
    </Html>
  )
}

export default WelcomeEmail
```

Preview with React Email’s CLI / app (`npx email dev` or your project’s create-email setup). Groundmail is not involved until you send.

---

## 3. Send with the SDK

```ts
import { Groundmail } from 'groundmail'
import { WelcomeEmail } from './emails/WelcomeEmail'

const client = new Groundmail(process.env.GROUNDMAIL_API_KEY!)

await client.emails.send({
  from: 'onboarding@yourdomain.com',
  to: 'user@example.com',
  subject: 'Welcome',
  react: WelcomeEmail({ name: 'Ada' }),
})
```

The SDK calls `@react-email/render` and POSTs `{ from, to, subject, html }` to `https://api.groundmail.dev/emails`.

**Rules:**

- `react` is mutually exclusive with `html`, `text`, and `template`
- Server-side only — never put `gm_` in the browser
- Same auth, sandbox, and domain rules as any other send ([Quickstart](https://docs.groundmail.dev/quickstart), [Agent skill notes](https://docs.groundmail.dev/ai/agent-skill))

---

## 4. Next.js (Route Handler)

```ts
// app/api/send-welcome/route.ts
import { NextResponse } from 'next/server'
import { Groundmail } from 'groundmail'
import { WelcomeEmail } from '@/emails/WelcomeEmail'

const tx = new Groundmail(process.env.GROUNDMAIL_API_KEY!)

export async function POST(request: Request) {
  const { to, name } = (await request.json()) as { to: string; name: string }

  const result = await tx.emails.send({
    from: 'onboarding@yourdomain.com',
    to,
    subject: 'Welcome',
    react: WelcomeEmail({ name }),
  })

  return NextResponse.json(result)
}
```

More Next.js patterns: [Next.js framework guide](https://docs.groundmail.dev/frameworks/nextjs).

---

## 5. Keep templates in your repo

Rendering at send time means your app must redeploy to change copy. If you want your
components to stay in git but let the send happen by alias — so a teammate can trigger
sends, and non-engineers can read what went out — push the rendered output to a template:

```bash
groundmail templates push ./emails/WelcomeEmail.tsx \
  --alias welcome \
  --subject "Welcome" \
  --props '{"name":"Ada"}' \
  --publish
```

The CLI imports the file **in your process**, renders it with `@react-email/render`, and
uploads the resulting HTML and text. Your component source never leaves your machine.

- `--alias` is the key. The command creates the template on first push and updates it after.
- `--subject` is required only when creating.
- `--props` supplies props for rendering. Use `{{variable}}` placeholders in the output for
  values that should be substituted per send.
- `--publish` cuts a new published version, which is what `template: { id: 'welcome' }` sends.
- `.html` and `.txt` files work too; `--text ./welcome.txt` attaches a hand-written text part.

TypeScript and JSX files need a loader. Install `tsx` (`npm install -D tsx`) or pre-render to
`.html`.

Then send by alias from anywhere, with no component import:

```ts
await client.emails.send({
  from: 'onboarding@yourdomain.com',
  to: 'user@example.com',
  template: { id: 'welcome', variables: { name: 'Ada' } },
})
```

Wire the push into CI to keep the platform in sync with `main`.

---

## 6. Already have HTML?

If you render elsewhere (or export from React Email yourself), send `html` instead of `react`:

```ts
await client.emails.send({
  from: 'onboarding@yourdomain.com',
  to: 'user@example.com',
  subject: 'Welcome',
  html: '<p>Hi Ada</p>',
})
```

Or use a published [dashboard template](https://docs.groundmail.dev/templates) with `template: { id | alias, variables }`.

---

## Troubleshooting

| Symptom | Fix |
|---------|-----|
| Error about `@react-email/render` | `npm install @react-email/render react` |
| `403` unverified domain | Verify DNS or use sandbox `from` / account `to` |
| Want MCP / agents | [MCP server](https://docs.groundmail.dev/ai/mcp) · [Agent Skills](https://docs.groundmail.dev/ai/agent-skills) — agents should link React Email’s own skills for authoring |

---

## FAQ

### Can I use React Email without Resend?

Yes. React Email is an open-source authoring toolchain. Groundmail’s Node SDK accepts `react` the same way many teams expect from a modern send API: install `groundmail` + `@react-email/render`, pass your component to `emails.send({ react })`, and we deliver HTML over our transactional infrastructure. Preview still uses React Email’s CLI.

### Is `react` the same as dashboard templates?

No. `react` renders in **your** Node process at send time (or you push rendered HTML with `groundmail templates push`). Dashboard templates are stored aliases with variables for non-engineers and multi-language SDKs. Many teams use React Email in git for critical flows and aliases for stable receipts.

### Why did send fail with a peer dependency error?

The `react` option requires `@react-email/render` (and `react`) installed in the app. Without that peer, use `html`, `text`, or `template` instead — or run `npm install @react-email/render react`.

---

## Related

- [Templates API](https://docs.groundmail.dev/templates) — server-side aliases and variables
- [Free email templates](https://docs.groundmail.dev/guides/email-templates) — 12 ready-made React Email components (auth, account, billing)
- [Password reset emails](https://docs.groundmail.dev/guides/password-reset-emails)
- [Password reset emails going to spam? (Learn)](https://groundmail.dev/guides/password-reset-emails-going-to-spam/)
- [Google, Yahoo and Microsoft sender requirements (Learn)](https://groundmail.dev/guides/google-yahoo-microsoft-sender-requirements/)
- [Node.js](https://docs.groundmail.dev/frameworks/node) · [Next.js](https://docs.groundmail.dev/frameworks/nextjs)
- React Email docs: [react.email](https://react.email)
