|
1
|
import { Table, Prerequisites } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `SendGrid Notification Module Provider`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
The SendGrid Notification Module Provider integrates [SendGrid](https://sendgrid.com) to send emails to users and customers. |
|
10
|
|
|
11
|
<Note title="Looking for managed email service?"> |
|
12
|
|
|
13
|
Cloud offers [Medusa Emails](!cloud!/emails), a managed email service that allows you to send transactional emails with zero configuration. It is built on top of the Notification Module and provides an easy way to manage email notifications in your Cloud projects with insights and deliverability features. |
|
14
|
|
|
15
|
</Note> |
|
16
|
|
|
17
|
## Register the SendGrid Notification Module |
|
18
|
|
|
19
|
<Prerequisites |
|
20
|
items={[ |
|
21
|
{ |
|
22
|
text: "SendGrid account", |
|
23
|
link: "https://signup.sendgrid.com", |
|
24
|
}, |
|
25
|
{ |
|
26
|
text: "Setup SendGrid single sender", |
|
27
|
link: "https://docs.sendgrid.com/ui/sending-email/sender-verification", |
|
28
|
}, |
|
29
|
{ |
|
30
|
text: "SendGrid API Key", |
|
31
|
link: "https://docs.sendgrid.com/ui/account-and-settings/api-keys", |
|
32
|
}, |
|
33
|
]} |
|
34
|
/> |
|
35
|
|
|
36
|
Add the module into the `providers` array of the Notification Module: |
|
37
|
|
|
38
|
<Note> |
|
39
|
|
|
40
|
Only one provider can be defined for a channel. |
|
41
|
|
|
42
|
</Note> |
|
43
|
|
|
44
|
```ts title="medusa-config.ts" |
|
45
|
module.exports = defineConfig({ |
|
46
|
// ... |
|
47
|
modules: [ |
|
48
|
{ |
|
49
|
resolve: "@medusajs/medusa/notification", |
|
50
|
options: { |
|
51
|
providers: [ |
|
52
|
// ... |
|
53
|
{ |
|
54
|
resolve: "@medusajs/medusa/notification-sendgrid", |
|
55
|
id: "sendgrid", |
|
56
|
options: { |
|
57
|
channels: ["email"], |
|
58
|
api_key: process.env.SENDGRID_API_KEY, |
|
59
|
from: process.env.SENDGRID_FROM, |
|
60
|
}, |
|
61
|
}, |
|
62
|
], |
|
63
|
}, |
|
64
|
}, |
|
65
|
], |
|
66
|
}) |
|
67
|
``` |
|
68
|
|
|
69
|
### Environment Variables |
|
70
|
|
|
71
|
Make sure to add the following environment variables: |
|
72
|
|
|
73
|
```bash |
|
74
|
SENDGRID_API_KEY=<YOUR_SENDGRID_API_KEY> |
|
75
|
SENDGRID_FROM=<YOUR_SENDGRID_FROM> |
|
76
|
``` |
|
77
|
|
|
78
|
### SendGrid Notification Module Options |
|
79
|
|
|
80
|
<Table> |
|
81
|
<Table.Header> |
|
82
|
<Table.Row> |
|
83
|
<Table.HeaderCell> |
|
84
|
Option |
|
85
|
</Table.HeaderCell> |
|
86
|
<Table.HeaderCell> |
|
87
|
Description |
|
88
|
</Table.HeaderCell> |
|
89
|
</Table.Row> |
|
90
|
</Table.Header> |
|
91
|
<Table.Body> |
|
92
|
<Table.Row> |
|
93
|
<Table.Cell>`channels`</Table.Cell> |
|
94
|
<Table.Cell> |
|
95
|
The channels this notification module is used to send notifications for. |
|
96
|
Only one provider can be defined for a channel. |
|
97
|
</Table.Cell> |
|
98
|
</Table.Row> |
|
99
|
<Table.Row> |
|
100
|
<Table.Cell>`api_key`</Table.Cell> |
|
101
|
<Table.Cell>The SendGrid API key.</Table.Cell> |
|
102
|
</Table.Row> |
|
103
|
<Table.Row> |
|
104
|
<Table.Cell>`from`</Table.Cell> |
|
105
|
<Table.Cell>The SendGrid from email.</Table.Cell> |
|
106
|
</Table.Row> |
|
107
|
</Table.Body> |
|
108
|
</Table> |
|
109
|
|
|
110
|
## SendGrid Templates |
|
111
|
|
|
112
|
When you send a notification, you must specify the ID of the template to use in SendGrid. |
|
113
|
|
|
114
|
Refer to [this SendGrid documentation guide](https://docs.sendgrid.com/ui/sending-email/how-to-send-an-email-with-dynamic-templates) on how to create templates for your different email types. |
|
115
|
|
|
116
|
## Advanced SendGrid Features |
|
117
|
|
|
118
|
### Custom Personalizations |
|
119
|
|
|
120
|
<Note> |
|
121
|
|
|
122
|
This feature is available since Medusa [v2.14.0](https://github.com/medusajs/medusa/releases/tag/v2.14.0). |
|
123
|
|
|
124
|
</Note> |
|
125
|
|
|
126
|
You can use SendGrid's [personalizations](https://docs.sendgrid.com/api-reference/mail-send/mail-send#request-body-parameters-personalizations) feature to send customized emails to multiple recipients or include additional SendGrid-specific options. |
|
127
|
|
|
128
|
Personalizations are useful for scenarios like: |
|
129
|
|
|
130
|
- Sending batch emails with different content for each recipient |
|
131
|
- Setting custom subject lines per recipient |
|
132
|
- Adding CC/BCC recipients for specific emails |
|
133
|
- Including additional headers or metadata |
|
134
|
|
|
135
|
Pass the personalizations in the `provider_data` when creating a notification: |
|
136
|
|
|
137
|
```ts |
|
138
|
await notificationModuleService.createNotifications({ |
|
139
|
channel: "email", |
|
140
|
template: "custom-template", |
|
141
|
provider_data: { |
|
142
|
personalizations: [ |
|
143
|
{ |
|
144
|
to: [{ email: "recipient1@example.com", name: "John Doe" }], |
|
145
|
dynamic_template_data: { |
|
146
|
name: "John", |
|
147
|
customField: "value1", |
|
148
|
}, |
|
149
|
subject: "Custom Subject for John", |
|
150
|
}, |
|
151
|
{ |
|
152
|
to: [{ email: "recipient2@example.com", name: "Jane Smith" }], |
|
153
|
dynamic_template_data: { |
|
154
|
name: "Jane", |
|
155
|
customField: "value2", |
|
156
|
}, |
|
157
|
subject: "Custom Subject for Jane", |
|
158
|
}, |
|
159
|
], |
|
160
|
}, |
|
161
|
}) |
|
162
|
``` |
|
163
|
|
|
164
|
When `personalizations` are provided, they take precedence over the `to` field and `data` field in the notification. Each personalization can include: |
|
165
|
|
|
166
|
- `to`: Array of recipient objects with email and optional name |
|
167
|
- `dynamic_template_data`: Template variables specific to this recipient group |
|
168
|
- `subject`: Custom subject line for this personalization |
|
169
|
- Other SendGrid personalization options like `cc`, `bcc`, `headers`, etc. |
|
170
|
|
|
171
|
<Note> |
|
172
|
|
|
173
|
You still need to pass `to` at the root level of the `createNotifications` parameter, but it will be ignored when `personalizations` are provided. |
|
174
|
|
|
175
|
</Note> |
|
176
|
|
|
177
|
--- |
|
178
|
|
|
179
|
## Test out the Module |
|
180
|
|
|
181
|
To test the module out, you'll listen to the `product.created` event and send an email when a product is created. |
|
182
|
|
|
183
|
Create a [subscriber](!docs!/learn/fundamentals/events-and-subscribers) at `src/subscribers/product-created.ts` with the following content: |
|
184
|
|
|
185
|
export const highlights = [ |
|
186
|
["14", "notificationModuleService", "Resolve the Notification Module."], |
|
187
|
["19", "product", "Retrieve the product's details."], |
|
188
|
["27", "createNotifications", "Create the notification to be sent."], |
|
189
|
[ |
|
190
|
"29", |
|
191
|
'"email"', |
|
192
|
"By specifying the `email` channel, SendGrid will be used to send the notification.", |
|
193
|
], |
|
194
|
["30", '"product-created"', "The ID of the template defined in SendGrid."], |
|
195
|
["31", "data", "The data to pass to the template defined in SendGrid."], |
|
196
|
] |
|
197
|
|
|
198
|
```ts title="src/subscribers/product-created.ts" highlights={highlights} collapsibleLines="1-10" expandButtonLabel="Show Imports" |
|
199
|
import type { |
|
200
|
SubscriberArgs, |
|
201
|
SubscriberConfig, |
|
202
|
} from "@medusajs/framework" |
|
203
|
import { |
|
204
|
Modules, |
|
205
|
ContainerRegistrationKeys, |
|
206
|
} from "@medusajs/framework/utils" |
|
207
|
|
|
208
|
export default async function productCreateHandler({ |
|
209
|
event: { data }, |
|
210
|
container, |
|
211
|
}: SubscriberArgs<{ id: string }>) { |
|
212
|
const notificationModuleService = container.resolve(Modules.NOTIFICATION) |
|
213
|
const query = container.resolve( |
|
214
|
ContainerRegistrationKeys.QUERY |
|
215
|
) |
|
216
|
|
|
217
|
const { data: [product] } = await query.graph({ |
|
218
|
entity: "product", |
|
219
|
fields: ["*"], |
|
220
|
filters: { |
|
221
|
id: data.id, |
|
222
|
}, |
|
223
|
}) |
|
224
|
|
|
225
|
await notificationModuleService.createNotifications({ |
|
226
|
to: "test@gmail.com", |
|
227
|
channel: "email", |
|
228
|
template: "product-created", |
|
229
|
data: { |
|
230
|
product_title: product.title, |
|
231
|
product_image: product.images[0]?.url, |
|
232
|
}, |
|
233
|
}) |
|
234
|
} |
|
235
|
|
|
236
|
export const config: SubscriberConfig = { |
|
237
|
event: "product.created", |
|
238
|
} |
|
239
|
``` |
|
240
|
|
|
241
|
In this subscriber, you: |
|
242
|
|
|
243
|
- Resolve the Notification Module's main service and [Query](!docs!/learn/fundamentals/query) from the [Medusa container](!docs!/learn/fundamentals/medusa-container). |
|
244
|
- Retrieve the product's details using Query to pass them to the template in SendGrid. |
|
245
|
- Use the `createNotifications` method of the Notification Module's main service to create a notification to be sent to the specified email. By specifying the `email` channel, the SendGrid Notification Module Provider is used to send the notification. |
|
246
|
- The `template` property of the `createNotifications` method's parameter specifies the ID of the template defined in SendGrid. |
|
247
|
- The `data` property allows you to pass data to the template in SendGrid. For example, the product's title and image. |
|
248
|
|
|
249
|
Then, start the Medusa application: |
|
250
|
|
|
251
|
```bash npm2yarn |
|
252
|
npm run dev |
|
253
|
``` |
|
254
|
|
|
255
|
And create a product either using the [API route](!api!/admin/products/create-product) or the [Medusa Admin](!user-guide!/products/create). This runs the subscriber and sends an email using SendGrid. |
|
256
|
|
|
257
|
### Other Events to Handle |
|
258
|
|
|
259
|
Medusa emits other events that you can handle to send notifications using the SendGrid Notification Module Provider, such as `order.placed` when an order is placed. |
|
260
|
|
|
261
|
Refer to the [Events Reference](/references/events) for a complete list of events emitted by Medusa. |
|
262
|
|
|
263
|
### Sending Emails with SendGrid in Workflows |
|
264
|
|
|
265
|
You can also send an email using SendGrid in any [workflow](!docs!/learn/fundamentals/workflows). This allows you to send emails within your custom flows. |
|
266
|
|
|
267
|
You can use the [sendNotificationsStep](/references/medusa-workflows/steps/sendNotificationsStep) in your workflow to send an email using SendGrid. |
|
268
|
|
|
269
|
For example: |
|
270
|
|
|
271
|
```ts title="src/workflows/send-email.ts" |
|
272
|
import { |
|
273
|
createWorkflow, |
|
274
|
transform, |
|
275
|
} from "@medusajs/framework/workflows-sdk" |
|
276
|
import { |
|
277
|
sendNotificationsStep, |
|
278
|
useQueryGraphStep, |
|
279
|
} from "@medusajs/medusa/core-flows" |
|
280
|
|
|
281
|
type WorkflowInput = { |
|
282
|
id: string |
|
283
|
} |
|
284
|
|
|
285
|
export const sendEmailWorkflow = createWorkflow( |
|
286
|
"send-email-workflow", |
|
287
|
({ id }: WorkflowInput) => { |
|
288
|
const { data: products } = useQueryGraphStep({ |
|
289
|
entity: "product", |
|
290
|
fields: [ |
|
291
|
"*", |
|
292
|
"variants.*", |
|
293
|
], |
|
294
|
filters: { |
|
295
|
id, |
|
296
|
}, |
|
297
|
}) |
|
298
|
|
|
299
|
const notificationData = transform( |
|
300
|
{ products }, |
|
301
|
({ products }) => ({ |
|
302
|
product_title: products[0].title, |
|
303
|
product_image: products[0].images[0]?.url, |
|
304
|
}) |
|
305
|
) |
|
306
|
|
|
307
|
sendNotificationsStep({ |
|
308
|
to: "test@gmail.com", |
|
309
|
channel: "email", |
|
310
|
template: "product-created", |
|
311
|
data: notificationData, |
|
312
|
}) |
|
313
|
} |
|
314
|
) |
|
315
|
``` |
|
316
|
|
|
317
|
This workflow works similarly to the subscriber. It retrieves the product's details using Query and sends an email using SendGrid (by specifying the `email` channel) to the `test@gmail.com` email. |
|
318
|
|
|
319
|
You can also execute this workflow in a subscriber. For example, you can execute it when a product is created: |
|
320
|
|
|
321
|
```ts title="src/subscribers/product-created.ts" |
|
322
|
import type { |
|
323
|
SubscriberArgs, |
|
324
|
SubscriberConfig, |
|
325
|
} from "@medusajs/framework" |
|
326
|
import { Modules } from "@medusajs/framework/utils" |
|
327
|
import { sendEmailWorkflow } from "../workflows/send-email" |
|
328
|
|
|
329
|
export default async function productCreateHandler({ |
|
330
|
event: { data }, |
|
331
|
container, |
|
332
|
}: SubscriberArgs<{ id: string }>) { |
|
333
|
await sendEmailWorkflow(container).run({ |
|
334
|
input: { |
|
335
|
id: data.id, |
|
336
|
}, |
|
337
|
}) |
|
338
|
} |
|
339
|
|
|
340
|
export const config: SubscriberConfig = { |
|
341
|
event: "product.created", |
|
342
|
} |
|
343
|
``` |
|
344
|
|
|
345
|
This subscriber will run every time a product is created, and it will execute the `sendEmailWorkflow` to send an email using SendGrid. |