# @xtr-dev/payload-mailing 📧 **Template-based email system with scheduling and job processing for PayloadCMS** ## Features ✅ **Template System**: Create reusable email templates with Handlebars syntax ✅ **Outbox Scheduling**: Schedule emails for future delivery ✅ **Job Integration**: Automatic processing via PayloadCMS jobs queue ✅ **Retry Failed Sends**: Automatic retry mechanism for failed emails ✅ **Template Variables**: Dynamic content with validation ✅ **Developer API**: Simple methods for sending emails programmatically ## Installation ```bash npm install @xtr-dev/payload-mailing ``` ## Quick Start ### 1. Add the plugin to your Payload config ```typescript import { buildConfig } from 'payload/config' import { mailingPlugin } from '@xtr-dev/payload-mailing' export default buildConfig({ // ... your config plugins: [ mailingPlugin({ defaultFrom: 'noreply@yoursite.com', transport: { host: 'smtp.gmail.com', port: 587, secure: false, auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASS, }, }, retryAttempts: 3, retryDelay: 300000, // 5 minutes queue: 'email-queue', // optional }), ], }) ``` ### 2. Send emails in your code ```typescript import { sendEmail, scheduleEmail } from '@xtr-dev/payload-mailing' // Send immediately const emailId = await sendEmail(payload, { templateId: 'welcome-email', to: 'user@example.com', variables: { firstName: 'John', welcomeUrl: 'https://yoursite.com/welcome' } }) // Schedule for later const scheduledId = await scheduleEmail(payload, { templateId: 'reminder-email', to: 'user@example.com', scheduledAt: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours variables: { eventName: 'Product Launch', eventDate: new Date('2024-01-15') } }) ``` ## Configuration ### Plugin Options ```typescript interface MailingPluginConfig { collections?: { templates?: string // default: 'email-templates' outbox?: string // default: 'email-outbox' } defaultFrom?: string transport?: Transporter | MailingTransportConfig queue?: string // default: 'default' retryAttempts?: number // default: 3 retryDelay?: number // default: 300000 (5 minutes) } ``` ### Transport Configuration You can provide either a Nodemailer transporter instance or configuration: ```typescript // Using configuration object { transport: { host: 'smtp.gmail.com', port: 587, secure: false, auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASS, }, } } // Or using a transporter instance import nodemailer from 'nodemailer' { transport: nodemailer.createTransporter({ // your config }) } ``` ## Creating Email Templates 1. Go to your Payload admin panel 2. Navigate to **Mailing > Email Templates** 3. Create a new template with: - **Name**: Descriptive name for the template - **Subject**: Email subject (supports Handlebars) - **HTML Template**: HTML content with Handlebars syntax - **Text Template**: Plain text version (optional) - **Variables**: Define available variables ### Template Example **Subject**: `Welcome to {{siteName}}, {{firstName}}!` **HTML Template**: ```html
Thanks for joining {{siteName}}. We're excited to have you!
{{#if isPremium}}Premium Benefits:
Your account was created on {{formatDate createdAt 'long'}}.
Visit your dashboard: Get Started
Best regards,
The {{siteName}} Team