@spree/docs 0.1.10 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4,11 +4,73 @@ sidebarTitle: Emails
|
|
|
4
4
|
description: Learn how to send transactional emails from your Spree application.
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
-
Spree
|
|
7
|
+
Spree handles two categories of emails:
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
| Category | Sent by | Examples |
|
|
10
|
+
|----------|---------|----------|
|
|
11
|
+
| **Customer-facing** | Storefront (via webhooks) | Order confirmation, shipping notification, password reset |
|
|
12
|
+
| **System/admin** | Spree backend (Rails) | Staff invitation, report ready, export complete |
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
## Customer-Facing Emails (Headless)
|
|
15
|
+
|
|
16
|
+
In headless builds, customer-facing emails are rendered and sent by the **storefront**, not the backend. The Spree backend publishes webhook events, and the storefront receives them, renders React email templates, and sends via [Resend](https://resend.com) (or any provider).
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
Spree Backend → Webhook POST → Storefront → render email → send via Resend
|
|
20
|
+
(HMAC signed) (verified) (react-email)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Setup
|
|
24
|
+
|
|
25
|
+
1. **Create a webhook endpoint** in Spree Admin → Settings → Developers → Webhooks:
|
|
26
|
+
- **URL:** `https://your-storefront.com/api/webhooks/spree`
|
|
27
|
+
- **Events:** `order.completed`, `order.canceled`, `order.shipped`, `customer.password_reset_requested`
|
|
28
|
+
|
|
29
|
+
2. **Configure the storefront** with the webhook secret and email provider:
|
|
30
|
+
|
|
31
|
+
```env
|
|
32
|
+
# .env.local (storefront)
|
|
33
|
+
SPREE_WEBHOOK_SECRET=your_webhook_endpoint_secret_key
|
|
34
|
+
RESEND_API_KEY=re_your_resend_api_key
|
|
35
|
+
EMAIL_FROM=Your Store <orders@your-domain.com>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
3. **The storefront handles everything else** — signature verification, event routing, email rendering, and delivery are built into `@spree/next/webhooks`. See the [Next.js storefront email docs](../storefront/nextjs/customization.md#transactional-emails) for template customization.
|
|
39
|
+
|
|
40
|
+
### Supported Events
|
|
41
|
+
|
|
42
|
+
| Event | Email |
|
|
43
|
+
|-------|-------|
|
|
44
|
+
| `order.completed` | Order confirmation with items, totals, addresses |
|
|
45
|
+
| `order.canceled` | Cancellation notice |
|
|
46
|
+
| `order.shipped` | Shipping notification with tracking link |
|
|
47
|
+
| `customer.password_reset_requested` | Password reset link |
|
|
48
|
+
|
|
49
|
+
### Custom Frameworks
|
|
50
|
+
|
|
51
|
+
If you're not using the Next.js storefront, you can build your own webhook handler with any framework. Use `@spree/sdk/webhooks` for signature verification:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { verifyWebhookSignature } from '@spree/sdk/webhooks'
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
See [Webhooks documentation](../core-concepts/webhooks.md) for the full payload format and verification details.
|
|
58
|
+
|
|
59
|
+
## System Emails (Rails)
|
|
60
|
+
|
|
61
|
+
System emails are internal notifications sent to **store staff**, not customers. They are sent by the Spree backend via Rails ActionMailer.
|
|
62
|
+
|
|
63
|
+
| Email | When |
|
|
64
|
+
|-------|------|
|
|
65
|
+
| Staff invitation | Admin invites a new team member |
|
|
66
|
+
| Invitation accepted | Invited user accepts |
|
|
67
|
+
| Report ready | Background report generation completes |
|
|
68
|
+
| Export complete | Data export finishes |
|
|
69
|
+
| Webhook endpoint disabled | Endpoint auto-disabled after repeated failures |
|
|
70
|
+
|
|
71
|
+
### Configuration
|
|
72
|
+
|
|
73
|
+
Set the following environment variables on the **Spree backend** to enable system email delivery:
|
|
12
74
|
|
|
13
75
|
| Variable | Default | Description |
|
|
14
76
|
| --- | --- | --- |
|
|
@@ -16,7 +78,7 @@ Set the following environment variables to enable email delivery:
|
|
|
16
78
|
| `SMTP_PORT` | `587` | SMTP server port |
|
|
17
79
|
| `SMTP_USERNAME` | — | SMTP auth username |
|
|
18
80
|
| `SMTP_PASSWORD` | — | SMTP auth password |
|
|
19
|
-
| `SMTP_FROM_ADDRESS` | — | Default "from" email address (e.g., `
|
|
81
|
+
| `SMTP_FROM_ADDRESS` | — | Default "from" email address (e.g., `admin@mystore.com`) |
|
|
20
82
|
| `RAILS_HOST` | `example.com` | Host used in email URLs |
|
|
21
83
|
|
|
22
84
|
When `SMTP_HOST` is not set, emails are printed to the Rails log instead of being sent.
|
|
@@ -29,7 +91,7 @@ When `SMTP_HOST` is not set, emails are printed to the Rails log instead of bein
|
|
|
29
91
|
SMTP_HOST=smtp.sendgrid.net
|
|
30
92
|
SMTP_USERNAME=apikey
|
|
31
93
|
SMTP_PASSWORD=SG.your-sendgrid-api-key
|
|
32
|
-
SMTP_FROM_ADDRESS=
|
|
94
|
+
SMTP_FROM_ADDRESS=admin@mystore.com
|
|
33
95
|
```
|
|
34
96
|
|
|
35
97
|
> **WARNING:** Remember to verify the email address in SendGrid you intend to use for sending, otherwise emails will be rejected.
|
|
@@ -41,7 +103,7 @@ When `SMTP_HOST` is not set, emails are printed to the Rails log instead of bein
|
|
|
41
103
|
SMTP_HOST=smtp.resend.com
|
|
42
104
|
SMTP_USERNAME=resend
|
|
43
105
|
SMTP_PASSWORD=re_your-resend-api-key
|
|
44
|
-
SMTP_FROM_ADDRESS=
|
|
106
|
+
SMTP_FROM_ADDRESS=admin@mystore.com
|
|
45
107
|
```
|
|
46
108
|
|
|
47
109
|
**Postmark:**
|
|
@@ -50,7 +112,7 @@ When `SMTP_HOST` is not set, emails are printed to the Rails log instead of bein
|
|
|
50
112
|
SMTP_HOST=smtp.postmarkapp.com
|
|
51
113
|
SMTP_USERNAME=your-postmark-server-token
|
|
52
114
|
SMTP_PASSWORD=your-postmark-server-token
|
|
53
|
-
SMTP_FROM_ADDRESS=
|
|
115
|
+
SMTP_FROM_ADDRESS=admin@mystore.com
|
|
54
116
|
```
|
|
55
117
|
|
|
56
118
|
**Amazon SES:**
|
|
@@ -59,19 +121,29 @@ When `SMTP_HOST` is not set, emails are printed to the Rails log instead of bein
|
|
|
59
121
|
SMTP_HOST=email-smtp.us-east-1.amazonaws.com
|
|
60
122
|
SMTP_USERNAME=your-ses-access-key-id
|
|
61
123
|
SMTP_PASSWORD=your-ses-secret-access-key
|
|
62
|
-
SMTP_FROM_ADDRESS=
|
|
124
|
+
SMTP_FROM_ADDRESS=admin@mystore.com
|
|
63
125
|
```
|
|
64
126
|
|
|
65
127
|
|
|
66
128
|
## Local Development
|
|
67
129
|
|
|
68
|
-
|
|
130
|
+
### Customer-facing emails (storefront)
|
|
131
|
+
|
|
132
|
+
In development, no email provider is needed. Emails are rendered to HTML files in `.next/emails/` with a clickable `file://` link in the console. To preview and design templates:
|
|
69
133
|
|
|
70
134
|
```bash
|
|
71
|
-
|
|
72
|
-
SMTP_HOST=localhost bin/dev
|
|
135
|
+
npm run email:dev
|
|
73
136
|
```
|
|
74
137
|
|
|
75
|
-
|
|
138
|
+
To test the full webhook flow locally, use [Cloudflare Tunnel](https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/):
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
brew install cloudflared
|
|
142
|
+
cloudflared tunnel --url http://localhost:3001
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Use the tunnel URL as the webhook endpoint URL in Spree Admin.
|
|
146
|
+
|
|
147
|
+
### System emails (Rails)
|
|
76
148
|
|
|
77
|
-
|
|
149
|
+
System emails use [letter_opener](https://github.com/ryanb/letter_opener) in development — emails open automatically in your browser instead of being sent.
|
|
@@ -17,6 +17,8 @@ These variables are required to run Spree in production.
|
|
|
17
17
|
|
|
18
18
|
## Email (SMTP)
|
|
19
19
|
|
|
20
|
+
> **TIP:** This configuration is used for system emails (e.g. staff invitations, report ready, export complete). Customer facing emails are handled by the [storefront via webhooks](../storefront/nextjs/customization.md#transactional-emails).
|
|
21
|
+
|
|
20
22
|
Spree works with any SMTP provider (Resend, Postmark, Mailgun, SendGrid, Amazon SES, etc.). Set `SMTP_HOST` to enable email delivery — when not set, emails are logged to stdout.
|
|
21
23
|
|
|
22
24
|
| Variable | Default | Description |
|
|
@@ -136,6 +136,66 @@ export default async function YourNewPage() {
|
|
|
136
136
|
}
|
|
137
137
|
```
|
|
138
138
|
|
|
139
|
+
## Transactional Emails
|
|
140
|
+
|
|
141
|
+
Customer-facing emails are rendered in the storefront using [react-email](https://react.email) and sent via [Resend](https://resend.com). The Spree backend delivers order/shipment/password events to the storefront via [webhooks](../../core-concepts/webhooks.md).
|
|
142
|
+
|
|
143
|
+
### Templates
|
|
144
|
+
|
|
145
|
+
Email templates are React components in `src/lib/emails/`:
|
|
146
|
+
|
|
147
|
+
| File | Event | Description |
|
|
148
|
+
|------|-------|-------------|
|
|
149
|
+
| `order-confirmation.tsx` | `order.completed` | Items, totals, addresses, delivery method |
|
|
150
|
+
| `order-canceled.tsx` | `order.canceled` | Cancellation notice with items |
|
|
151
|
+
| `shipment-shipped.tsx` | `order.shipped` | Tracking number and link |
|
|
152
|
+
| `password-reset.tsx` | `customer.password_reset_requested` | Reset button and fallback link |
|
|
153
|
+
|
|
154
|
+
Customize templates by editing these files directly. They use `@react-email/components` for email-safe layout primitives.
|
|
155
|
+
|
|
156
|
+
### Previewing
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
npm run email:dev
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Opens the react-email dev server with mock data for all templates at `http://localhost:3000`.
|
|
163
|
+
|
|
164
|
+
### Webhook Handler
|
|
165
|
+
|
|
166
|
+
The webhook route (`src/app/api/webhooks/spree/route.ts`) uses `createWebhookHandler` from `@spree/next/webhooks`:
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
import { createWebhookHandler } from '@spree/next/webhooks'
|
|
170
|
+
|
|
171
|
+
export const POST = createWebhookHandler({
|
|
172
|
+
secret: process.env.SPREE_WEBHOOK_SECRET!,
|
|
173
|
+
handlers: {
|
|
174
|
+
'order.completed': handleOrderCompleted,
|
|
175
|
+
'order.canceled': handleOrderCanceled,
|
|
176
|
+
'order.shipped': handleOrderShipped,
|
|
177
|
+
'customer.password_reset_requested': handlePasswordReset,
|
|
178
|
+
},
|
|
179
|
+
})
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
To add a new email type:
|
|
183
|
+
|
|
184
|
+
1. Create a template in `src/lib/emails/`
|
|
185
|
+
2. Add a handler function in `src/lib/webhooks/handlers.ts`
|
|
186
|
+
3. Register the event in `route.ts`
|
|
187
|
+
4. Subscribe to the event in Spree Admin → Webhooks
|
|
188
|
+
|
|
189
|
+
### Local Development
|
|
190
|
+
|
|
191
|
+
In dev, emails are written to `.next/emails/` as HTML files — no Resend key needed. Use [Cloudflare Tunnel](https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/) to receive webhooks locally:
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
cloudflared tunnel --url http://localhost:3001
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
For full setup details, see [Sending out Emails](../../deployment/emails.md).
|
|
198
|
+
|
|
139
199
|
## Building a Custom Storefront
|
|
140
200
|
|
|
141
201
|
If you prefer to build from scratch instead of forking the starter, you can use the `@spree/next` and `@spree/sdk` packages directly in any Next.js application. See the [@spree/next Package](spree-next-package.md) documentation for the complete API reference.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spree/docs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"description": "Spree Commerce developer documentation for AI agents and local reference",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "CC-BY-4.0",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"clean": "rm -rf dist"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"vitest": "^4.1.
|
|
27
|
+
"vitest": "^4.1.2"
|
|
28
28
|
},
|
|
29
29
|
"engines": {
|
|
30
30
|
"node": ">=18.0.0"
|