@rokmohar/medusa-plugin-usesend
v2.3.0
Published
useSend email notification provider for Medusa 2, with React email templates, attachments, retries and rate limiting
Downloads
1,171
Maintainers
Readme
useSend plugin for Medusa
Send Medusa email notifications through useSend, the open-source email platform formerly called Unsend. Write your emails as React components, and the plugin renders and sends them.
- Features
- Compatibility
- Installation
- Quick start
- Configuration
- Email templates
- Sending emails
- Attachments
- Testing without sending
- Delivery webhooks
- Contacts
- Admin page
- Migrating from @rokmohar/medusa-plugin-unsend
- Self-hosting useSend
- Changelog
- Contributing
- Security
- License
Features
- Notification provider for Medusa's notification module, on the
emailchannel. - React email templates, loaded from a folder and rendered to HTML and plain text with your app's own React.
- Translated templates:
OrderPlaced.sl.tsxis picked for Slovenian customers. - useSend dashboard templates, sent by id with variables.
- Per-email options: reply-to, CC, BCC, headers and scheduled sending.
- Attachments from Medusa's
createNotificationscall. - Safe retries: temporary failures are retried with backoff, and an idempotency key keeps a retry from sending the email twice.
- Mailpit, dry run and redirect modes for development and staging.
- Delivery webhooks: bounces, complaints, opens and clicks become Medusa events.
- Contacts: add newsletter subscribers to a useSend contact book.
- Admin page to preview templates and send yourself a test.
- Rate limiting per minute, and per-environment overrides of any option.
- Works with useSend Cloud and self-hosted useSend.
Compatibility
| Plugin | Medusa |
| ----------------------------------------- | --------------- |
| @rokmohar/medusa-plugin-usesend 2.3.x | 2.23 or newer |
| @rokmohar/medusa-plugin-usesend 2.0–2.2 | 2.22 |
| @rokmohar/medusa-plugin-unsend 1.1.x | 2.19 – 2.21 |
| @rokmohar/medusa-plugin-unsend 1.0.8 | 2.4 – 2.18 |
| @rokmohar/medusa-plugin-unsend 0.2.4 | 2.3.1 and older |
Node.js 22.22 or newer is required, the same as Medusa 2.23.
Installation
npm install @rokmohar/medusa-plugin-usesendOr with Yarn:
yarn add @rokmohar/medusa-plugin-usesendQuick start
Add your useSend credentials to
.env:USESEND_API_KEY=us_... USESEND_FROM=Shop <[email protected]> # Only for self-hosted useSend; useSend Cloud is the default USESEND_URL=Register the plugin and the notification provider in
medusa-config.ts:import { defineConfig, loadEnv } from '@medusajs/framework/utils' import type { UseSendEmailOptions } from '@rokmohar/medusa-plugin-usesend' loadEnv(process.env.NODE_ENV || 'development', process.cwd()) module.exports = defineConfig({ // ... other config plugins: [ { resolve: '@rokmohar/medusa-plugin-usesend', options: { url: process.env.USESEND_URL, api_key: process.env.USESEND_API_KEY ?? '', from: process.env.USESEND_FROM ?? '', } satisfies UseSendEmailOptions, }, ], modules: [ { resolve: '@medusajs/medusa/notification', dependencies: ['usesend'], options: { providers: [ { resolve: '@rokmohar/medusa-plugin-usesend/providers/notification', id: 'usesend', options: { channels: ['email'], }, }, ], }, }, ], })Add a template to
src/templates/emails(see Email templates) and send it from a subscriber or workflow (see Sending emails).
The provider path is also exported as a constant, if you prefer it over the string:
import { USESEND_PROVIDER_PATH } from '@rokmohar/medusa-plugin-usesend'
// ...
{
resolve: USESEND_PROVIDER_PATH,
id: 'usesend',
options: { channels: ['email'] },
}Configuration
| Option | Type | Default | Description |
| ------------------------ | --------- | ---------------------------- | --------------------------------------------------------------------- |
| api_key | string | required | useSend API key. Not needed with mailpitUrl or dryRun. |
| from | string | required | Default sender, e.g. Shop <[email protected]>. |
| url | string | https://app.usesend.com | API URL. Set it to your instance's URL when you self-host useSend. |
| templateDir | string | <cwd>/src/templates/emails | Folder the email templates are loaded from. |
| retry.maxAttempts | number | 3 | Attempts per email, including the first one. |
| retry.delay | number | 1000 | Delay before the first retry in ms. |
| retry.backoff | string | 'exponential' | 'exponential' doubles the delay on every retry, 'linear' adds it. |
| rateLimit.maxPerMinute | number | 60 | Maximum emails sent per minute. Further emails wait. |
| defaultLocale | string | none | Locale used when a notification has none, see Locales. |
| dryRun | boolean | false | Log emails instead of sending them. |
| mailpitUrl | string | none | Send every email to this Mailpit instead, see below. |
| redirectTo | string | none | Send every email to this address instead. |
| webhook_secret | string | none | Signing secret that turns on delivery webhooks. |
| contacts.contactBookId | string | none | Default contact book for contacts. |
| environment | object | none | Overrides of any option above per NODE_ENV, see below. |
The options are validated at startup, and an invalid configuration stops the server with a message listing every problem.
Per-environment overrides
environment takes development, staging and production keys. The overrides of the current NODE_ENV are
merged over the top-level options:
options: {
api_key: process.env.USESEND_API_KEY ?? '',
from: 'Shop <[email protected]>',
environment: {
development: {
from: 'Shop (dev) <[email protected]>',
rateLimit: { maxPerMinute: 10 },
},
production: {
retry: { maxAttempts: 5, delay: 2000 },
},
},
}Email templates
At startup the plugin loads every .tsx and .js file in the template folder. Each file's default export is a React
component that receives the notification's data as props:
// src/templates/emails/OrderPlaced.tsx
type Props = {
display_id: number
customer_name: string
}
const OrderPlaced = ({ display_id, customer_name }: Props) => {
return (
<div>
<h1>Thanks for your order, {customer_name}!</h1>
<p>Your order #{display_id} has been received.</p>
</div>
)
}
OrderPlaced.Subject = 'Your order has been received'
export default OrderPlaced- The template name is the file name in kebab case:
OrderPlaced.tsxbecomesorder-placed. - Templates are rendered to HTML with your app's
reactandreact-dom, so they work with React Email components. - In production, Medusa compiles the templates to
.jsin.medusa/server/src/templates/emails, and the plugin loads those.
Subject
The subject is taken from the first of these that is set:
content.subjectin thecreateNotificationscallsubjectin the template's metadata file- The component's static
Subjectproperty - The template name, e.g.
order-placed
Metadata file
A JSON file with the same name next to the template is optional:
// src/templates/emails/OrderPlaced.json
{
"version": "1.0.0",
"subject": "Your order has been received",
"description": "Sent to the customer after checkout",
"tags": ["order", "customer"],
"category": "orders"
}Locales
Add a locale to the file name for a translated variant: OrderPlaced.sl.tsx is the Slovenian order-placed, and
OrderPlaced.sl.json holds its metadata, on top of OrderPlaced.json.
The locale is read from provider_data.locale, or else data.locale, of the notification. The plugin uses the first
variant that exists:
- The exact locale, e.g.
sl-si - Its language, e.g.
sl - The
defaultLocaleoption - The template without a locale
Preview data
The admin page fills a template with sample data. Set it as a static PreviewProps on the component,
the same as React Email does, or as previewProps in the metadata file:
OrderPlaced.PreviewProps = {
display_id: 1001,
customer_name: 'Ana',
}A broken template doesn't keep the others from loading: the plugin logs which file failed and why.
Sending emails
Send an email through Medusa's notification module, with the template name and the data for its props:
// src/subscribers/order-placed.ts
import type { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'
import { Modules } from '@medusajs/framework/utils'
export default async function orderPlacedHandler({ event: { data }, container }: SubscriberArgs<{ id: string }>) {
const query = container.resolve('query')
const notificationModuleService = container.resolve(Modules.NOTIFICATION)
const {
data: [order],
} = await query.graph({
entity: 'order',
fields: ['display_id', 'email', 'shipping_address.first_name'],
filters: { id: data.id },
})
await notificationModuleService.createNotifications({
to: order.email,
channel: 'email',
template: 'order-placed',
data: {
display_id: order.display_id,
customer_name: order.shipping_address?.first_name,
},
})
}
export const config: SubscriberConfig = {
event: 'order.placed',
}Every email has an HTML and a plain-text part. The content of the notification overrides the template: content.html
and content.text replace what the template renders, and an email with content.subject and content.html needs no
template at all.
Per-email options
Pass options for one email in provider_data:
await notificationModuleService.createNotifications({
to: order.email,
channel: 'email',
template: 'order-placed',
data: { display_id: order.display_id },
provider_data: {
replyTo: '[email protected]',
bcc: '[email protected]',
headers: { 'X-Entity-Ref-ID': order.id },
scheduledAt: '2026-11-01T09:00:00Z',
locale: 'sl',
},
})replyTo, cc and bcc take an address or a list of addresses. Invalid provider_data fails the notification
without sending it.
useSend dashboard templates
To send a template you edit in the useSend dashboard instead of a file, pass its id and variables. Nothing is rendered
locally, and the subject comes from the useSend template unless content.subject is set:
await notificationModuleService.createNotifications({
to: customer.email,
channel: 'email',
template: 'welcome',
provider_data: {
templateId: 'tmpl_123',
variables: { firstName: customer.first_name },
},
})The usesend service
The plugin's service is registered in the container as usesend. Use it to register templates in code instead of
files, or to check that a template exists:
import type { UseSendService } from '@rokmohar/medusa-plugin-usesend'
const usesend = container.resolve<UseSendService>('usesend')
usesend.addTemplate('welcome', {
subject: 'Welcome!',
content: { html: '<p>Welcome to our shop.</p>' },
})Attachments
Pass attachments in the attachments field of createNotifications. Each one needs a filename and base64-encoded
content:
await notificationModuleService.createNotifications({
to: '[email protected]',
channel: 'email',
template: 'invoice',
attachments: [
{
filename: 'invoice-42.pdf',
content: pdfBuffer.toString('base64'),
},
],
})useSend only accepts filename and content, so other fields of Medusa's attachment (content_type, disposition,
id) are ignored. Attachments in data.attachments are still read, for older code.
Testing without sending
Three options keep test emails away from real customers. All of them can be set per environment:
mailpitUrl: 'http://localhost:8025'sends every email to Mailpit, a local inbox, see below.dryRun: truelogs each email (recipient, subject and template) instead of sending it.redirectTo: '[email protected]'sends every email to one address. The original recipients go into anX-Original-Toheader, and CC and BCC are dropped.
options: {
api_key: process.env.USESEND_API_KEY ?? '',
from: 'Shop <[email protected]>',
environment: {
development: { dryRun: true },
staging: { redirectTo: '[email protected]' },
},
}Mailpit
Mailpit catches the emails in a web inbox, where you can read the HTML and plain text
and open the attachments. Run it next to your database, e.g. in docker-compose.yml:
services:
mailpit:
image: axllent/mailpit:latest
ports:
- '127.0.0.1:8025:8025'Then set mailpitUrl for local development. The plugin sends through Mailpit's HTTP API, so no useSend API key is
needed, and the inbox is at http://localhost:8025:
options: {
api_key: process.env.USESEND_API_KEY,
from: 'Shop <[email protected]>',
mailpitUrl: process.env.MAILPIT_URL, // http://localhost:8025 in your local .env
}Each email is tagged with its template name. A useSend dashboard template can't be rendered locally, so it arrives as text with the template id and variables.
Keep a local Mailpit bound to localhost, because it accepts email from anyone who can reach it. To use a Mailpit on a
server, for example for staging, protect it with logins: MP_UI_AUTH_FILE for the inbox, and MP_SEND_API_AUTH for
a login that can only send. Then put the send login in the URL:
MAILPIT_URL=https://sender:s3cret%[email protected]The plugin sends it as a Basic auth header, and the admin page shows the URL without it. Percent-encode special
characters in the login: Medusa's loadEnv expands $, so write $ as %24.
Delivery webhooks
The plugin turns useSend webhook calls into Medusa events, so you can react to bounces, complaints, opens and clicks.
- In the useSend dashboard, add a webhook pointing at
https://<your-medusa>/hooks/usesendand copy its signing secret. - Set the
webhook_secretoption to it. Without it, the route responds 404.
Every call is verified and emitted as usesend.<type>, e.g. usesend.email.bounced. The event names are exported as
USESEND_WEBHOOK_EVENTS:
// src/subscribers/email-bounced.ts
import type { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'
import { USESEND_WEBHOOK_EVENTS, type UseSendWebhookEventData } from '@rokmohar/medusa-plugin-usesend'
export default async function emailBounced({ event, container }: SubscriberArgs<UseSendWebhookEventData>) {
const { notification_id, to, data } = event.data
container.resolve('logger').warn(`Email ${notification_id} to ${to.join(', ')} bounced: ${JSON.stringify(data)}`)
}
export const config: SubscriberConfig = {
event: USESEND_WEBHOOK_EVENTS.EMAIL_BOUNCED,
}The event data holds call_id, type, email_id, notification_id (the Medusa notification that sent the email,
when there is one), to, status and the raw useSend data. useSend can deliver a call more than once, so use
call_id to skip duplicates.
Contacts
Add the people who sign up for your newsletter to a useSend contact book, so you can send them campaigns from useSend. The plugin doesn't sync customers on its own, because Medusa has no newsletter consent; call it from your own sign-up flow:
const usesend = container.resolve<UseSendService>('usesend')
await usesend.upsertContact({
email: '[email protected]',
firstName: 'Ana',
subscribed: true,
properties: { locale: 'sl' },
})upsertContact creates the contact, or updates the one with the same email, in the contact book of the
contacts.contactBookId option, or the one passed as the second argument. removeContact(contactId) removes one.
Admin page
Settings → useSend in the Medusa admin lists the loaded templates and shows whether the plugin is live, in dry run
or redirecting. For each template you can edit the sample data, pick a locale, preview the HTML and plain text, and
send a test to yourself. Test emails go through the notification module, so they show up in the notification log and
follow dryRun and redirectTo.
Migrating from @rokmohar/medusa-plugin-unsend
Unsend was renamed to useSend, and this plugin was renamed with it in 2.0.0. Your templates and options don't change.
Swap the package:
npm uninstall @rokmohar/medusa-plugin-unsend npm install @rokmohar/medusa-plugin-usesendRename in your code and
medusa-config.ts:| 1.x (
@rokmohar/medusa-plugin-unsend) | 2.x (@rokmohar/medusa-plugin-usesend) | | -------------------------------------------------------------------- | ----------------------------------------------------------------------- | |resolve: '@rokmohar/medusa-plugin-unsend'|resolve: '@rokmohar/medusa-plugin-usesend'| |'@rokmohar/medusa-plugin-unsend/providers/notification'|'@rokmohar/medusa-plugin-usesend/providers/notification'| |UNSEND_PROVIDER_PATH|USESEND_PROVIDER_PATH| |dependencies: ['unsend']|dependencies: ['usesend']| |container.resolve('unsend')|container.resolve('usesend')| |UnsendService|UseSendService| |UnsendEmailOptions,UnsendEmailTemplate,UnsendEmailAttachment|UseSendEmailOptions,UseSendEmailTemplate,UseSendEmailAttachment|Optionally rename the provider
idfromunsendtousesend. Medusa stores the provider id on each sent notification, so keepunsendif you rely on filtering past notifications by provider.Optionally rename your
UNSEND_*environment variables toUSESEND_*. The plugin only reads the values you pass in its options.
Self-hosting useSend
useSend can run on your own server; it needs PostgreSQL, Redis and AWS SES. Follow the
self-hosting guide, then set url to your instance, e.g.
http://localhost:3000.
Changelog
See CHANGELOG.md and the GitHub releases.
Contributing
Bug reports, documentation fixes and pull requests are welcome. See CONTRIBUTING.md for the development setup and the code of conduct.
Security
Please report vulnerabilities privately, as described in SECURITY.md.
License
MIT © Rok Mohar
