Unsend plugin for Medusa 2
Run the following command to install the plugin with npm:
npm install --save @rokmohar/medusa-plugin-unsend
Or with yarn:
yarn add @rokmohar/medusa-plugin-unsend
This plugin is only for MedusaJS v2.4.0 or newer.
If you are using MedusaJS v2.3.1 or older, please use the older version of this plugin.
Add the plugin to your file:
import { loadEnv, defineConfig } from '@medusajs/framework/utils'import { UNSEND_PROVIDER_PATH } from '@rokmohar/medusa-plugin-unsend'loadEnv(process.env.NODE_ENV || 'development', process.cwd())module.exports = defineConfig({// ... other configplugins: [// ... other plugins{resolve: '@rokmohar/medusa-plugin-unsend',options: {// Required optionsurl: process.env.UNSEND_URL ?? '',api_key: process.env.UNSEND_API_KEY ?? '',from: process.env.UNSEND_FROM ?? '',// Optional configurationtemplateDir: 'custom/templates/path', // Custom template directoryretry: {maxAttempts: 5, // Number of retry attempts for failed sendsdelay: 2000, // Delay between retries in milliseconds},rateLimit: {maxPerMinute: 30, // Maximum emails per minute},// Environment configurations based on NODE_ENVenvironment: {development: {// Development-specific overridesfrom: 'dev@example.com',rateLimit: {maxPerMinute: 25,},},staging: {// Staging-specific overridesfrom: 'stage@example.com',rateLimit: {maxPerMinute: 50,},},production: {// Production-specific overridesfrom: 'prod@example.com',rateLimit: {maxPerMinute: 100,},},},},},],modules: [// ... other modules{resolve: '@medusajs/medusa/notification',dependencies: ['unsend'],options: {providers: [// ... other providers{resolve: UNSEND_PROVIDER_PATH,id: 'unsend',options: {channels: ['email'],},},],},},],})
Add the environment variables to your and file:
# ... others varsUNSEND_URL=UNSEND_API_KEY=UNSEND_FROM=
If you want to use with the from this README, use the following values:
# ... others varsUNSEND_URL=http://localhost:3000UNSEND_API_KEY=test_123456789UNSEND_FROM=no-reply@example.org
The plugin automatically loads email templates from the directory in your project root (or a custom directory specified in the configuration). Each template consists of two files:
// src/templates/emails/ProductUpsert.tsximport React from 'react'const ProductUpsertEmail = (props: any) => {return (<div><h1>Product Updated</h1><p>Product ID: {props.productId}</p></div>)}// Set email subjectProductUpsertEmail.Subject = 'Products upserted'export default ProductUpsertEmail
The email subject is determined in the following order of precedence:
For example, if you have a template named , the subject will fall back to if no other subject is specified.
// src/templates/emails/ProductUpsert.json{"version": "1.0.0","subject": "Product upsert","description": "Email template for product updates","tags": ["product", "update"],"category": "product-notifications"}
The template name will be derived from the filename (without the .tsx extension). For example, will be available as the template named .
The JSON file is optional and can contain the following fields:
The email subject is determined in the following order of precedence:
For example, if you have a template named , the subject will fall back to if no other subject is specified.
You must add the following subscribers to the :
import { SubscriberArgs, SubscriberConfig } from '@medusajs/framework'import { IProductModuleService } from '@medusajs/framework/types'import { Modules } from '@medusajs/framework/utils'import { ProductEvents, SearchUtils } from '@medusajs/utils'import { UnsendService } from '@rokmohar/medusa-plugin-unsend/core'export default async function productCreatedHandler({ event: { data }, container }: SubscriberArgs<{ id: string }>) {const productId = data.id// Make sure template is registered, before creating email notificationsconst unsendService: UnsendService = container.resolve('unsend')const notificationModuleService = container.resolve(Modules.NOTIFICATION)await notificationModuleService.createNotifications({to: 'first.last@example.org',channel: 'email',// Use name of the registered templatetemplate: 'product-upsert',// Set email subjectcontent: {subject: 'Product upserted',},})}export const config: SubscriberConfig = {event: [ProductEvents.PRODUCT_CREATED, ProductEvents.PRODUCT_UPDATED],}
You can add the following configuration for Unsend to your :
services:# ... other servicesunsend:image: 'unsend/unsend:latest'port:- '3000:3000'