# Groundmail with Medusa

Use [`medusa-notification-groundmail`](https://www.npmjs.com/package/medusa-notification-groundmail) as the `email` channel of the Medusa v2 Notification Module. Order confirmations, shipping updates and password resets then go out through Groundmail.

## 1. Install

```bash
npm install medusa-notification-groundmail
```

```bash
GROUNDMAIL_API_KEY=gm_your_key_here
```

## 2. Register the provider

```ts
// medusa-config.ts
module.exports = defineConfig({
  // ...
  modules: [
    {
      resolve: '@medusajs/medusa/notification',
      options: {
        providers: [
          {
            resolve: 'medusa-notification-groundmail',
            id: 'groundmail',
            options: {
              channels: ['email'],
              api_key: process.env.GROUNDMAIL_API_KEY,
              from: 'Acme <orders@yourdomain.com>',
            },
          },
        ],
      },
    },
  ],
})
```

The `from` address must be on a domain you have [verified in Groundmail](https://docs.groundmail.dev/domains).

## 3. Send a notification

From a subscriber or workflow step:

```ts
import { Modules } from '@medusajs/framework/utils'

const notificationModuleService = container.resolve(Modules.NOTIFICATION)

await notificationModuleService.createNotifications({
  to: order.email,
  channel: 'email',
  template: 'order-placed',
  data: { customer_name: order.customer?.first_name, order: { display_id: order.display_id } },
})
```

### Templates or your own markup

- **Groundmail template (default).** Without `content`, the provider sends the [Groundmail template](https://docs.groundmail.dev/templates) whose id or alias matches `template`, with `data` as its variables. Nested keys are joined with underscores: `{{order_display_id}}` reads `data.order.display_id`. Lists of plain values are joined with commas.
- **Your own markup.** Pass `content: { subject, html, text }` and the provider sends it as-is.

Use the `templates` option to point Medusa template names at Groundmail templates with different ids.

## Options

| Option | Purpose |
|--------|---------|
| `api_key` | Groundmail API key. Falls back to `GROUNDMAIL_API_KEY` |
| `from` | Default From address, on a verified domain |
| `reply_to` | Default Reply-To address |
| `templates` | Map Medusa template names to Groundmail templates, e.g. `{ "order-placed": "order_confirmation" }` |
| `base_url` | Override the API base URL |

A notification's `provider_data` can carry `cc`, `bcc`, `replyTo`, `tags`, `headers`, `idempotencyKey` and `scheduledAt`.

## Source

[Groundmail/medusa-notification-groundmail](https://github.com/Groundmail/medusa-notification-groundmail)
