Back to Plugins

SMTP Notification Provider

A robust SMTP email notification provider for MedusaJS, enabling reliable email delivery through any SMTP service provider for transactional and marketing emails.


Overview

This plugin integrates SMTP email functionality into your MedusaJS store using Nodemailer. It supports any SMTP service provider including Gmail, SendGrid, Mailgun, and custom SMTP servers, providing a flexible solution for email communications.

Features

Requirements

Installation

Install the plugin and nodemailer:

npm install @tsc_tech/medusa-plugin-smtp nodemailer

Configuration

Step 1: Choose SMTP Provider

Select an SMTP service provider:

Step 2: Get SMTP Credentials

Obtain credentials from your provider:

Gmail Example:

SendGrid Example:

Step 3: Environment Variables

Add your SMTP credentials to .env:

# SMTP Server Configuration
SMTP_HOST=smtp.gmail.com
SMTP_PORT=465
SMTP_SECURE=true  # true for 465, false for other ports

# SMTP Authentication
SMTP_AUTH_USER=your-email@gmail.com
SMTP_AUTH_PASS=your-app-password

# Email Settings
SMTP_NAME=YourStoreName
SMTP_FROM=your-email@gmail.com
DEFAULT_REPLY_TO=support@yourstore.com

Step 4: Plugin Configuration

Add the plugin to your medusa-config.ts:

import { Modules } from "@medusajs/framework/utils"

module.exports = defineConfig({
  // ... other config
  modules: {
    [Modules.NOTIFICATION]: {
      resolve: "@medusajs/medusa/notification",
      options: {
        providers: [
          {
            resolve: "@tsc_tech/medusa-plugin-smtp",
            id: "smtp",
            options: {
              channels: ["email"],
              transport: {
                host: process.env.SMTP_HOST,
                port: process.env.SMTP_PORT,
                secure: process.env.SMTP_SECURE === "true",
                auth: {
                  user: process.env.SMTP_AUTH_USER,
                  pass: process.env.SMTP_AUTH_PASS,
                },
              },
              from: process.env.SMTP_FROM,
              name: process.env.SMTP_NAME,
              reply_to: process.env.DEFAULT_REPLY_TO,
            },
          },
        ],
      },
    },
  },
})

Usage

Sending Emails

Use the notification service to send emails:

import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"

export async function POST(req: MedusaRequest, res: MedusaResponse) {
  const notificationModuleService = req.scope.resolve("notification")

  await notificationModuleService.createNotifications({
    to: "customer@example.com",
    channel: "email",
    template: "order-placed",
    data: {
      // Email data
      subject: "Order Confirmation",
      // ... other template data
    },
  })

  res.json({ success: true })
}

Email Types

Common email notifications:

SMTP Provider Setup Guides

Gmail Configuration

  1. Enable 2-factor authentication on your Google account
  2. Generate an app-specific password:
    • Go to Google Account Settings
    • Security → App passwords
    • Create a new app password
  3. Use app password in SMTP_AUTH_PASS
SMTP_HOST=smtp.gmail.com
SMTP_PORT=465
SMTP_SECURE=true
SMTP_AUTH_USER=youremail@gmail.com
SMTP_AUTH_PASS=your-16-digit-app-password

SendGrid Configuration

  1. Create SendGrid account and verify sender
  2. Generate API key from Settings → API Keys
  3. Use API key as password:
SMTP_HOST=smtp.sendgrid.net
SMTP_PORT=587
SMTP_SECURE=false
SMTP_AUTH_USER=apikey
SMTP_AUTH_PASS=your-sendgrid-api-key

Mailgun Configuration

  1. Add and verify your domain in Mailgun
  2. Get SMTP credentials from domain settings:
SMTP_HOST=smtp.mailgun.org
SMTP_PORT=587
SMTP_SECURE=false
SMTP_AUTH_USER=postmaster@your-domain.com
SMTP_AUTH_PASS=your-mailgun-smtp-password

Port Configuration

Common SMTP ports:

Use SMTP_SECURE=true for port 465, false for others.

Email Templates

Create email templates for different events:

// templates/order-placed.tsx
export default function OrderPlaced({ order }) {
  return (
    <html>
      <body>
        <h1>Order Confirmation</h1>
        <p>Thank you for your order #{order.display_id}</p>
        {/* ... email content */}
      </body>
    </html>
  )
}

Testing

Development Testing

For development, you can use:

Test Configuration

# Mailtrap example
SMTP_HOST=smtp.mailtrap.io
SMTP_PORT=2525
SMTP_SECURE=false
SMTP_AUTH_USER=your-mailtrap-username
SMTP_AUTH_PASS=your-mailtrap-password

Troubleshooting

Common Issues

Authentication Failed:

Connection Timeout:

Emails Not Sending:

Best Practices

  1. Use App Passwords - Never use your main account password
  2. SSL/TLS - Always use secure connections
  3. Sender Verification - Verify your sending domain
  4. Rate Limits - Be aware of provider sending limits
  5. Error Handling - Implement proper error handling
  6. Email Validation - Validate recipient email addresses
  7. Unsubscribe - Include unsubscribe links for marketing emails

Explore More Medusa Plugins

Find more powerful plugins and integrations to enhance your Medusa store. Browse our collection of community-driven solutions.

Free & Open Source
Community Driven
Easy Integration