mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 08:13:23 +00:00
Add mailing plugin with templates, outbox, and job processing
This commit is contained in:
180
src/collections/EmailOutbox.ts
Normal file
180
src/collections/EmailOutbox.ts
Normal file
@@ -0,0 +1,180 @@
|
||||
import { CollectionConfig } from 'payload/types'
|
||||
|
||||
const EmailOutbox: CollectionConfig = {
|
||||
slug: 'email-outbox',
|
||||
admin: {
|
||||
useAsTitle: 'subject',
|
||||
defaultColumns: ['subject', 'to', 'status', 'scheduledAt', 'sentAt'],
|
||||
group: 'Mailing',
|
||||
},
|
||||
access: {
|
||||
read: () => true,
|
||||
create: () => true,
|
||||
update: () => true,
|
||||
delete: () => true,
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'template',
|
||||
type: 'relationship',
|
||||
relationTo: 'email-templates',
|
||||
admin: {
|
||||
description: 'Email template used (optional if custom content provided)',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'to',
|
||||
type: 'text',
|
||||
required: true,
|
||||
admin: {
|
||||
description: 'Recipient email address(es), comma-separated',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'cc',
|
||||
type: 'text',
|
||||
admin: {
|
||||
description: 'CC email address(es), comma-separated',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'bcc',
|
||||
type: 'text',
|
||||
admin: {
|
||||
description: 'BCC email address(es), comma-separated',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'from',
|
||||
type: 'text',
|
||||
admin: {
|
||||
description: 'Sender email address (optional, uses default if not provided)',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'replyTo',
|
||||
type: 'text',
|
||||
admin: {
|
||||
description: 'Reply-to email address',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'subject',
|
||||
type: 'text',
|
||||
required: true,
|
||||
admin: {
|
||||
description: 'Email subject line',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'html',
|
||||
type: 'textarea',
|
||||
required: true,
|
||||
admin: {
|
||||
description: 'Rendered HTML content of the email',
|
||||
rows: 8,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'text',
|
||||
type: 'textarea',
|
||||
admin: {
|
||||
description: 'Plain text version of the email',
|
||||
rows: 6,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'variables',
|
||||
type: 'json',
|
||||
admin: {
|
||||
description: 'Template variables used to render this email',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'scheduledAt',
|
||||
type: 'date',
|
||||
admin: {
|
||||
description: 'When this email should be sent (leave empty for immediate)',
|
||||
date: {
|
||||
pickerAppearance: 'dayAndTime',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'sentAt',
|
||||
type: 'date',
|
||||
admin: {
|
||||
description: 'When this email was actually sent',
|
||||
date: {
|
||||
pickerAppearance: 'dayAndTime',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'status',
|
||||
type: 'select',
|
||||
required: true,
|
||||
options: [
|
||||
{ label: 'Pending', value: 'pending' },
|
||||
{ label: 'Processing', value: 'processing' },
|
||||
{ label: 'Sent', value: 'sent' },
|
||||
{ label: 'Failed', value: 'failed' },
|
||||
],
|
||||
defaultValue: 'pending',
|
||||
admin: {
|
||||
description: 'Current status of this email',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'attempts',
|
||||
type: 'number',
|
||||
defaultValue: 0,
|
||||
admin: {
|
||||
description: 'Number of send attempts made',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'lastAttemptAt',
|
||||
type: 'date',
|
||||
admin: {
|
||||
description: 'When the last send attempt was made',
|
||||
date: {
|
||||
pickerAppearance: 'dayAndTime',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'error',
|
||||
type: 'textarea',
|
||||
admin: {
|
||||
description: 'Last error message if send failed',
|
||||
rows: 3,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'priority',
|
||||
type: 'number',
|
||||
defaultValue: 5,
|
||||
admin: {
|
||||
description: 'Email priority (1=highest, 10=lowest)',
|
||||
},
|
||||
},
|
||||
],
|
||||
timestamps: true,
|
||||
indexes: [
|
||||
{
|
||||
fields: {
|
||||
status: 1,
|
||||
scheduledAt: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
fields: {
|
||||
priority: -1,
|
||||
createdAt: 1,
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
export default EmailOutbox
|
||||
105
src/collections/EmailTemplates.ts
Normal file
105
src/collections/EmailTemplates.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import { CollectionConfig } from 'payload/types'
|
||||
|
||||
const EmailTemplates: CollectionConfig = {
|
||||
slug: 'email-templates',
|
||||
admin: {
|
||||
useAsTitle: 'name',
|
||||
defaultColumns: ['name', 'subject', 'updatedAt'],
|
||||
group: 'Mailing',
|
||||
},
|
||||
access: {
|
||||
read: () => true,
|
||||
create: () => true,
|
||||
update: () => true,
|
||||
delete: () => true,
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'name',
|
||||
type: 'text',
|
||||
required: true,
|
||||
admin: {
|
||||
description: 'A descriptive name for this email template',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'subject',
|
||||
type: 'text',
|
||||
required: true,
|
||||
admin: {
|
||||
description: 'Email subject line (supports Handlebars variables)',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'htmlTemplate',
|
||||
type: 'textarea',
|
||||
required: true,
|
||||
admin: {
|
||||
description: 'HTML email template (supports Handlebars syntax)',
|
||||
rows: 10,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'textTemplate',
|
||||
type: 'textarea',
|
||||
admin: {
|
||||
description: 'Plain text email template (supports Handlebars syntax)',
|
||||
rows: 8,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'variables',
|
||||
type: 'array',
|
||||
admin: {
|
||||
description: 'Define variables that can be used in this template',
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'name',
|
||||
type: 'text',
|
||||
required: true,
|
||||
admin: {
|
||||
description: 'Variable name (e.g., "firstName", "orderTotal")',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'type',
|
||||
type: 'select',
|
||||
required: true,
|
||||
options: [
|
||||
{ label: 'Text', value: 'text' },
|
||||
{ label: 'Number', value: 'number' },
|
||||
{ label: 'Boolean', value: 'boolean' },
|
||||
{ label: 'Date', value: 'date' },
|
||||
],
|
||||
defaultValue: 'text',
|
||||
},
|
||||
{
|
||||
name: 'required',
|
||||
type: 'checkbox',
|
||||
defaultValue: false,
|
||||
admin: {
|
||||
description: 'Is this variable required when sending emails?',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'description',
|
||||
type: 'text',
|
||||
admin: {
|
||||
description: 'Optional description of what this variable represents',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'previewData',
|
||||
type: 'json',
|
||||
admin: {
|
||||
description: 'Sample data for previewing this template (JSON format)',
|
||||
},
|
||||
},
|
||||
],
|
||||
timestamps: true,
|
||||
}
|
||||
|
||||
export default EmailTemplates
|
||||
Reference in New Issue
Block a user