Integration Module 2.0 for Medusa is outRead the announcement
Usesend logo

Usesend

useSend email notification provider for Medusa 2, with React email templates, attachments, retries and rate limiting

npm install @rokmohar/medusa-plugin-usesend
Built by
Service
Version
2.2.1
Last updated
2 weeks ago
Monthly downloads
Loading downloads

useSend plugin for Medusa

npm version npm downloads CI License: MIT

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

  • Notification provider for Medusa's notification module, on the channel.
  • React email templates, loaded from a folder and rendered to HTML and plain text with your app's own React.
  • Translated templates: is 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 call.
  • 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

PluginMedusa
2.x2.22 or newer
1.1.x2.19 – 2.21
1.0.82.4 – 2.18
0.2.42.3.1 and older

Node.js 22.22 or newer is required, the same as Medusa 2.22.

Installation

Or with Yarn:

Quick start

  1. Add your useSend credentials to :

  2. Register the plugin and the notification provider in :

  3. Add a template to (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:

Configuration

OptionTypeDefaultDescription
requireduseSend API key. Not needed with or .
requiredDefault sender, e.g. .
API URL. Set it to your instance's URL when you self-host useSend.
Folder the email templates are loaded from.
Attempts per email, including the first one.
Delay before the first retry in ms.
doubles the delay on every retry, adds it.
Maximum emails sent per minute. Further emails wait.
noneLocale used when a notification has none, see Locales.
Log emails instead of sending them.
noneSend every email to this Mailpit instead, see below.
noneSend every email to this address instead.
noneSigning secret that turns on delivery webhooks.
noneDefault contact book for contacts.
noneOverrides of any option above per , see below.

The options are validated at startup, and an invalid configuration stops the server with a message listing every problem.

Per-environment overrides

takes , and keys. The overrides of the current are merged over the top-level options:

Email templates

At startup the plugin loads every and file in the template folder. Each file's default export is a React component that receives the notification's as props:

  • The template name is the file name in kebab case: becomes .
  • Templates are rendered to HTML with your app's and , so they work with React Email components.
  • In production, Medusa compiles the templates to in , and the plugin loads those.

Subject

The subject is taken from the first of these that is set:

  1. in the call
  2. in the template's metadata file
  3. The component's static property
  4. The template name, e.g.

Metadata file

A JSON file with the same name next to the template is optional:

Locales

Add a locale to the file name for a translated variant: is the Slovenian , and holds its metadata, on top of .

The locale is read from , or else , of the notification. The plugin uses the first variant that exists:

  1. The exact locale, e.g.
  2. Its language, e.g.
  3. The option
  4. The template without a locale

Preview data

The admin page fills a template with sample data. Set it as a static on the component, the same as React Email does, or as in the metadata file:

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:

Every email has an HTML and a plain-text part. The of the notification overrides the template: and replace what the template renders, and an email with and needs no template at all.

Per-email options

Pass options for one email in :

, and take an address or a list of addresses. Invalid 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 is set:

The service

The plugin's service is registered in the container as . Use it to register templates in code instead of files, or to check that a template exists:

Attachments

Pass attachments in the field of . Each one needs a and base64-encoded :

useSend only accepts and , so other fields of Medusa's attachment (, , ) are ignored. Attachments in 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:

  • sends every email to Mailpit, a local inbox, see below.
  • logs each email (recipient, subject and template) instead of sending it.
  • sends every email to one address. The original recipients go into an header, and CC and BCC are dropped.

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 :

Then set for local development. The plugin sends through Mailpit's HTTP API, so no useSend API key is needed, and the inbox is at :

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: for the inbox, and for a login that can only send. Then put the send login in the URL:

MAILPIT_URL=https://sender:s3cret%24pass@mailpit.example.com

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 expands , so write as .

Delivery webhooks

The plugin turns useSend webhook calls into Medusa events, so you can react to bounces, complaints, opens and clicks.

  1. In the useSend dashboard, add a webhook pointing at and copy its signing secret.
  2. Set the option to it. Without it, the route responds 404.

Every call is verified and emitted as , e.g. . The event names are exported as :

The event data holds , , , (the Medusa notification that sent the email, when there is one), , and the raw useSend . useSend can deliver a call more than once, so use 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:

creates the contact, or updates the one with the same email, in the contact book of the option, or the one passed as the second argument. 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 and .

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.

  1. Swap the package:

  2. Rename in your code and :

    1.x ()2.x ()
    , , , ,
  3. Optionally rename the provider from to . Medusa stores the provider id on each sent notification, so keep if you rely on filtering past notifications by provider.

  4. Optionally rename your environment variables to . 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 to your instance, e.g. .

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