@gerbergpt/medusa-notifications-mailjet
v1.5.0
Published
Mailjet email delivery service for Medusa V2
Maintainers
Readme
Compatibility
This plugin is compatible with versions >= 2.11.x of @medusajs/medusa.
Table of Contents
Prerequisites
- Node.js v20 or higher
- Medusa server v2.11.3 or higher
- A Mailjet account and API credential
Installation
pnpm add @gerbergpt/medusa-notifications-mailjetConfiguration
Add the provider module in your medusa-config.ts file:
module.exports = defineConfig({
projectConfig: {
// ...
},
modules: [
// ... other modules
{
resolve: "@medusajs/medusa/notification",
options: {
providers: [
{
id: "notification-mailjet",
resolve: "@gerbergpt/medusa-notifications-mailjet/providers/notifications-mailjet",
options: {
channels: ["email"],
api_key: process.env.MAILJET_API_KEY,
api_secret: process.env.MAILJET_SECRET_KEY,
from_email: process.env.MAILJET_FROM,
from_name: process.env.MAILJET_FROM_NAME,
templates: {
"<template-name>": {
subject: "<subject-function>",
template: "<template-function>",
},
},
default_locale: "en",
},
},
],
},
},
]
})Environment Variables
Create or update your .env file with the following variables:
MAILJET_API_KEY="<your-mailgun-api-key>"
MAILJET_SECRET_KEY="<your-mailgun-api-key>"
MAILJET_FROM="<your-mailgun-from-email>"
MAILJET_FROM_NAME="<optional-from-name>"Mailjet's transactional API does not require a dedicated sending domain for this plugin, so no domain configuration is needed.
Usage
Install @react-email/components in your Medusa backend application.
npm i @react-email/components -DTo set up your email templates, you’ll need to create two functions for each template:
- One function that accepts a locale and returns the email subject.
- One function that accepts the email’s props and returns the email template.
For example:
- In your Medusa server’s src directory, create a new folder named emails.
- Inside the emails folder, create a file called order-placed.tsx.
- Add the following code to order-placed.tsx:
import * as React from "react"
import { Html, Head, Preview, Body, Container, Heading, Text } from "@react-email/components"
export const getOrderPlacedTemplate = () => (
<Html>
<Head/>
<Preview>Your order has been placed</Preview>
<Body>
<Container>
<Heading>Thanks for your order!</Heading>
<Text>Order #F001922 </Text>
<Text>Total: $10.00</Text>
</Container>
</Body>
</Html>
)
export const orderPlacedSubject = (locale: string) => {
switch (locale) {
case "zh":
return "订单确认"
case "en":
return "Order Placed"
}
}- In the
medusa-config.tsfile add the following code:
module.exports = defineConfig({
projectConfig: {
// ...
},
modules: [
// ... other modules
{
resolve: "@medusajs/medusa/notification",
options: {
providers: [
{
id: "notification-mailjet",
resolve: "@gerbergpt/medusa-mailjet/providers/notifications-mailjet",
options: {
channels: ["email"],
api_key: process.env.MAILJET_API_KEY,
api_secret: process.env.MAILJET_SECRET_KEY,
from_email: process.env.MAILJET_FROM,
from_name: process.env.MAILJET_FROM_NAME,
templates: {
"order-placed": {
subject: orderPlacedSubject,
template: getOrderPlacedTemplate,
},
},
default_locale: "en",
},
},
],
},
},
]
})Local development and customization
In case you want to customize and test the plugin locally, refer to the Medusa Plugin docs.
