Mailgun
Send and manage email notifications
Mailgun notifications plugin for Medusa V2
Send emails using Mailgun.
Table of Contents
Prerequisites
- Node.js v20 or higher
- Medusa server v2.3.0 or higher
- A Mailgun account and API key and domain
[!NOTE] If you use Mailgun's EU-hosted infrastructure, you need this url as the api_url: https://api.eu.mailgun.net
Installation
1pnpm add @webbers/mailgun-notification-medusa
Configuration
Add the provider to the Copy to clipboard@medusajs/payment module in your Copy to clipboardmedusa-config.ts file:
1234567891011121314151617181920module.exports = defineConfig({projectConfig: {// ...},modules: [// ... other modules{resolve: "@medusajs/medusa/notification",options: {providers: [{resolve:"@webbers/mailgun-notification-medusa/providers/notification-mailgun",id: "notification-mailgun",options: {channels: ["email"],apiKey: process.env.MAILGUN_API_KEY,domain: process.env.MAILGUN_DOMAIN,from_email: process.env.MAILGUN_FROM,api_url: process.env.MAILGUN_API_URL, // Only required if using Mailgun's EU-hosted infrastructure
Configuration Options
Option Description Default Copy to clipboardapiKey Your Mailgun API key Required Copy to clipboarddomain Your Mailgun domain Required Copy to clipboardfrom_email Your from email address Required Copy to clipboardtemplates Your email template functions Required Copy to clipboarddefault_locale The default locale for the emails Required Copy to clipboardapi_url The API url of mailgun Optional
Environment Variables
Create or update your Copy to clipboard.env file with the following variables:
1234MAILGUN_API_KEY="<your-mailgun-api-key>"MAILGUN_DOMAIN="<your-mailgun-domain>"MAILGUN_FROM="<your-mailgun-from-email>"MAILGUN_API_URL="<your-api-url>"
Usage
To set up up your email templates two functions are required per template:
- A function that takes a locale as a parameter and returns the subject of the email
- A function that takes the props of the email as a parameter and returns the template of the email For example you could set it up like this:
- In the src directory of your medusa server create a directory called Copy to clipboard
emails - Inside the emails directory create a file called Copy to clipboard
order-placed.tsx - Inside the order-placed.tsx file add the following code:
1234567891011121314151617181920import * as React from "react"import {Html,Head,Preview,Body,Container,Heading,Text,} from "@react-email/components"export const getOrderPlacedTemplate = () => (<Html><Head/><Preview>Your order is confirmed</Preview><Body><Container><Heading>Thanks for your order!</Heading><Text>Order #12345 has been confirmed.</Text><Text>Total: $59.99</Text>
- In the Copy to clipboard
medusa-config.tsfile add the following code:
1234567891011121314151617181920module.exports = defineConfig({projectConfig: {// ...},modules: [// ... other modules{resolve: "@medusajs/medusa/notification",options: {providers: [{resolve:"@webbers/mailgun-notification-medusa/providers/notification-mailgun",id: "notification-mailgun",options: {channels: ["email"],apiKey: process.env.MAILGUN_API_KEY,domain: process.env.MAILGUN_DOMAIN,from_email: process.env.MAILGUN_FROM,api_url: process.env.MAILGUN_API_URL, // Only required if using Mailgun's EU-hosted infrastructure
Local development and customization
In case you want to customize and test the plugin locally, refer to the Medusa Plugin docs.

