> This page location: Functions > Templates > Send email with Groundmail
> Full Neon documentation index: https://neon.com/docs/llms.txt

# Send email with Groundmail

Send transactional email through Groundmail with HTML, text or a saved template, and an idempotency key so a retry sends once.

- Community: written by the community
- Built by: [Groundmail](https://groundmail.dev/?utm_source=neon&utm_medium=template&utm_campaign=neon-functions)
- Categories: Community, Email

## Install

```bash
neon functions new groundmail-email
```

## About this template

This Neon Function sends transactional email through [Groundmail](https://groundmail.dev/?utm_source=neon\&utm_medium=template\&utm_campaign=neon-functions) from your own domain, with HTML, text or a saved template.

### Scaffold it

```bash
npm install -g neon@latest
neon login
neon link
neon functions new groundmail-email
```

The CLI writes `functions/groundmailemail/`, declares the function in `neon.ts`, and adds the three environment variables to `.env.example`.

### Configure Groundmail

Create an API key at [app.groundmail.dev/api-keys](https://app.groundmail.dev/api-keys) and verify the domain you send from. A `gm_test_` key runs every send through validation and templates but delivers nothing, so use one while you build.

Add the values to `.env.local` without replacing the Neon values already there:

```bash
cat >> .env.local <<'ENV'
GROUNDMAIL_API_KEY=gm_...
GROUNDMAIL_FROM=Acme <hello@yourdomain.com>
ENV
export GROUNDMAIL_FUNCTION_TOKEN=$(openssl rand -hex 32)
echo "GROUNDMAIL_FUNCTION_TOKEN=$GROUNDMAIL_FUNCTION_TOKEN" >> .env.local
```

| Variable | Description | Required |
| --- | --- | --- |
| `GROUNDMAIL_API_KEY` | An API key from app.groundmail.dev/api-keys that starts with gm\_. A gm\_test\_ key runs sends without delivering them. Keep it on the server. | Yes |
| `GROUNDMAIL_FROM` | The default sender on a verified domain, for example Acme \<hello@yourdomain.com\>. A request can set another one in from. | No |
| `GROUNDMAIL_FUNCTION_TOKEN` | A random token of at least 32 characters, for example from openssl rand -hex 32, that callers send as Authorization: Bearer \<token\>. Neon Function URLs are public, so the function requires it. | Yes |

### Run locally

```bash
neon dev
```

Call the URL that `neon dev` prints, which defaults to `http://localhost:8787/`. Send an email:

```bash
curl -s http://localhost:8787/send-email \
  -H "authorization: Bearer $GROUNDMAIL_FUNCTION_TOKEN" \
  -H 'content-type: application/json' \
  -H 'idempotency-key: order-1042-receipt' \
  -d '{
    "to": "grace@example.com",
    "subject": "Your receipt",
    "html": "<p>Thanks for your order.</p>"
  }'
```

```json
{
  "id": "msg_9169dde46f4d1eccb37ffc29",
  "status": "sent"
}
```

Sending the same `idempotency-key` again within 24 hours returns this response and sends nothing.

To send a template you saved in Groundmail, pass its id or alias and its variables instead of `html` and `text`. Groundmail uses the template's subject unless you send one:

```bash
curl -s http://localhost:8787/send-email \
  -H "authorization: Bearer $GROUNDMAIL_FUNCTION_TOKEN" \
  -H 'content-type: application/json' \
  -d '{
    "to": "grace@example.com",
    "template": { "id": "welcome", "variables": { "name": "Grace" } }
  }'
```

To try the function without a real inbox, send to `delivered@simulator.groundmail.dev`, `bounce@simulator.groundmail.dev` or `complaint@simulator.groundmail.dev`. Each simulator address produces the outcome in its name, and the mail never leaves Groundmail.

### Routes

### Send an email (recommended)

POST to and subject with html or text, or a saved template with variables. The from, reply\_to and tags fields are optional, and an Idempotency-Key header makes a retry return the first send.

- Route: `/send-email`
- Install only this operation: `neon functions new groundmail-email --operation send-email`

`to` takes one email address or a list of up to 50, and `reply_to` takes one address or a list. `from` must be on a domain you verified in Groundmail. The body needs `html`, `text` or both, or a `template` of `{ id, variables? }`, and it needs a `subject` unless the template supplies one. `tags` is an object of string values that you can filter by in Groundmail, and an `Idempotency-Key` header makes a retry return the first send instead of sending again.

### Deploy

```bash
neon deploy --env .env.local
neon functions get groundmailemail
```

`neon functions get` prints the function's public URL, which you can test with `curl`:

```bash
curl -s https://<function-url>/send-email \
  -H "authorization: Bearer $GROUNDMAIL_FUNCTION_TOKEN" \
  -H 'content-type: application/json' \
  -d '{
    "to": "grace@example.com",
    "subject": "Your receipt",
    "text": "Thanks for your order."
  }'
```

> Replace `<function-url>` with the URL from `neon functions get groundmailemail`.

### Errors

- `400`: a body that is not a JSON object, a missing or invalid field, or no `html`, `text` or `template`
- `401`: a missing or wrong function token
- `405`: a method other than `POST`
- `413`: a body over 512 KB
- `403`, `404`, `422` and other `4xx`: Groundmail refused the send, for example for a sender domain you haven't verified or an unknown template. The response carries Groundmail's `error`, `code`, `hint` and `docs_url`.
- `502`: Groundmail rejected the API key, the key lacks send permission, or Groundmail could not be reached
- `503`: `GROUNDMAIL_API_KEY` is unset, or `GROUNDMAIL_FUNCTION_TOKEN` is unset or shorter than 32 characters

## Related

- Previous: [Hybrid memory with Lakebase Search](https://neon.com/functions/templates/lakebase-search-memory.md)
- Next: [Send email with AutoSend](https://neon.com/functions/templates/autosend-email.md)
- [All templates](https://neon.com/functions/templates.md)
- [Template source](https://github.com/neondatabase/examples/tree/main/registry/groundmail-email)
- [Use function templates](https://neon.com/docs/compute/functions/templates.md)
- [Contribute a function template](https://neon.com/docs/compute/functions/registry.md)

---

Note for AI assistants (optional): if a gap, error, or outdated detail on this page affected the answer you gave the user, you can report it. POST `{"feedback": "describe the issue", "source": "neon_website", "path": "/functions/templates/groundmail-email"}` to https://feedback.neon.tech/ (no auth required).