Compare commits

..

46 Commits
dev ... v0.4.16

Author SHA1 Message Date
Bas
e95296feff Merge pull request #63 from xtr-dev/dev
Fix template population in beforeSend hook and bump version to 0.4.16
2025-10-06 23:23:14 +02:00
Bas
8406bca718 Merge pull request #62 from xtr-dev/dev
Dev
2025-10-06 23:16:24 +02:00
Bas
8e1128f1e8 Merge pull request #61 from xtr-dev/dev
Remove deprecated `processEmailsTask` and associated helpers
2025-09-27 11:49:44 +02:00
Bas
50ce181893 Merge pull request #59 from xtr-dev/dev
Dev
2025-09-20 20:29:07 +02:00
Bas
2c0f202518 Merge pull request #58 from xtr-dev/dev
Dev
2025-09-20 20:18:56 +02:00
Bas
aa5a03b5b0 Merge pull request #57 from xtr-dev/dev
Fix TypeScript build error in jobScheduler.ts
2025-09-20 19:04:46 +02:00
Bas
2220d83288 Merge pull request #56 from xtr-dev/dev
Dev
2025-09-20 19:01:01 +02:00
Bas
de1ae636de Merge pull request #55 from xtr-dev/dev
Dev
2025-09-14 23:36:58 +02:00
Bas
5e0ed0a03a Merge pull request #52 from xtr-dev/dev
Dev
2025-09-14 22:05:08 +02:00
Bas
060b1914b6 Merge pull request #46 from xtr-dev/dev
Dev
2025-09-14 20:45:26 +02:00
Bas
d82b3f2276 Merge pull request #45 from xtr-dev/dev
Fix TypeScript compilation error in MailingService
2025-09-14 20:03:42 +02:00
Bas
05f4cd0d7c Merge pull request #44 from xtr-dev/dev
Dev
2025-09-14 20:00:46 +02:00
Bas
bba223410d Merge pull request #43 from xtr-dev/dev
Remove verbose initialization logs
2025-09-14 18:36:19 +02:00
Bas
a40d87c63c Merge pull request #42 from xtr-dev/dev
BREAKING CHANGE: Remove sendEmailWorkflow, add immediate processing t…
2025-09-14 18:07:19 +02:00
Bas
fde8eb538d Merge pull request #41 from xtr-dev/dev
Dev
2025-09-14 17:47:19 +02:00
Bas
ff94d72d49 Merge pull request #40 from xtr-dev/dev
BREAKING CHANGE: Remove custom transport support, use Payload's email…
2025-09-14 17:02:50 +02:00
Bas
0083e8e1fa Merge pull request #39 from xtr-dev/dev
Remove redundant queueName validation and debug log, bump version to …
2025-09-14 16:29:13 +02:00
Bas
6cf055178b Merge pull request #38 from xtr-dev/dev
Add debug log for email transporter configuration and bump version to…
2025-09-14 16:15:41 +02:00
Bas
556d910e30 Merge pull request #37 from xtr-dev/dev
Remove conditional transporter initialization and bump version to 0.1.22
2025-09-14 13:53:32 +02:00
Bas
efdfaf5889 Merge pull request #36 from xtr-dev/dev
Add beforeSend hook for email customization
2025-09-14 12:37:38 +02:00
Bas
f12ac8172e Merge pull request #35 from xtr-dev/dev
Fix model overwrite error when plugin is initialized multiple times
2025-09-14 10:24:58 +02:00
Bas
672ab3236a Merge pull request #34 from xtr-dev/dev
Add fromName field support to emails collection
2025-09-14 00:10:22 +02:00
Bas
7f04275d39 Merge pull request #33 from xtr-dev/dev
Dev
2025-09-13 23:53:56 +02:00
Bas
ea87f14308 Merge pull request #32 from xtr-dev/dev
Dev
2025-09-13 23:48:28 +02:00
Bas
ff788c1ecf Merge pull request #31 from xtr-dev/dev
Fix variables field type to support all JSON-compatible values
2025-09-13 23:41:43 +02:00
Bas
72f3d7f66d Merge pull request #30 from xtr-dev/dev
Add null value support to BaseEmailDocument interface
2025-09-13 23:35:25 +02:00
Bas
5905f732de Merge pull request #29 from xtr-dev/dev
Support custom ID types (string/number) for improved compatibility
2025-09-13 23:24:55 +02:00
Bas
685875d1b9 Merge pull request #28 from xtr-dev/dev
Dev
2025-09-13 23:11:16 +02:00
Bas
768b70a003 Merge pull request #27 from xtr-dev/dev
Align `sendEmail` and `sendEmailTask` with updated `BaseEmail` typing
2025-09-13 22:49:05 +02:00
Bas
21b22a033a Merge pull request #26 from xtr-dev/dev
Refactor `sendEmail` to improve type safety and align with `BaseEmail…
2025-09-13 22:41:28 +02:00
Bas
03f1f62fbf Merge pull request #25 from xtr-dev/dev
Remove `emailWrapper` hook and all associated references.
2025-09-13 22:34:48 +02:00
Bas
e38b63d814 Merge pull request #24 from xtr-dev/dev
Dev
2025-09-13 22:00:51 +02:00
Bas
c78a8c2480 Merge pull request #23 from xtr-dev/dev
Fix TypeScript compatibility with PayloadCMS generated types
2025-09-13 21:10:09 +02:00
Bas
0c4d894f51 Merge pull request #22 from xtr-dev/dev
Move sendEmail to dedicated file for better visibility
2025-09-13 20:58:13 +02:00
Bas
6d4e020133 Merge pull request #21 from xtr-dev/dev
Dev
2025-09-13 20:39:44 +02:00
Bas
b3de54b953 Merge pull request #20 from xtr-dev/dev
Simplify job system architecture
2025-09-13 20:16:10 +02:00
Bas
ed058c0721 Merge pull request #19 from xtr-dev/dev
Dev
2025-09-13 19:23:08 +02:00
Bas
273dea5a73 Merge pull request #18 from xtr-dev/dev
🎨 Fix README features section formatting
2025-09-13 18:40:43 +02:00
Bas
c81ef7f8a8 Merge pull request #17 from xtr-dev/dev
🚀 BREAKING: Simplify API to use Payload collections directly
2025-09-13 18:36:11 +02:00
Bas
cddcfb1e4c Merge pull request #16 from xtr-dev/dev
Replace Handlebars with flexible template engine system
2025-09-13 18:11:06 +02:00
Bas
80d32674a9 Merge pull request #15 from xtr-dev/dev
Add automatic job scheduling and rescheduling
2025-09-13 17:00:33 +02:00
Bas
c7af628beb Merge pull request #14 from xtr-dev/dev
Add defaultFromName config option and bump to v0.0.7
2025-09-13 16:29:26 +02:00
Bas
048fa33747 Merge pull request #13 from xtr-dev/dev
Fix TypeScript build error with payload email adapter
2025-09-13 16:18:41 +02:00
Bas
cb62874500 Merge pull request #12 from xtr-dev/dev
Make mailer transport config optional, use Payload config fallback
2025-09-13 16:04:26 +02:00
Bas
9efea193b1 Merge pull request #11 from xtr-dev/dev
### 🔧 Improvements
2025-09-13 15:07:08 +02:00
Bas
c09d7d4fc5 Merge pull request #10 from xtr-dev/dev
### 📚 Documentation
2025-09-13 15:00:31 +02:00
7 changed files with 40 additions and 128 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@xtr-dev/payload-mailing", "name": "@xtr-dev/payload-mailing",
"version": "0.4.21", "version": "0.4.16",
"description": "Template-based email system with scheduling and job processing for PayloadCMS", "description": "Template-based email system with scheduling and job processing for PayloadCMS",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

@@ -12,7 +12,7 @@ const Emails: CollectionConfig = {
description: 'Email delivery and status tracking', description: 'Email delivery and status tracking',
}, },
defaultPopulate: { defaultPopulate: {
templateSlug: true, template: true,
to: true, to: true,
cc: true, cc: true,
bcc: true, bcc: true,
@@ -40,14 +40,6 @@ const Emails: CollectionConfig = {
description: 'Email template used (optional if custom content provided)', description: 'Email template used (optional if custom content provided)',
}, },
}, },
{
name: 'templateSlug',
type: 'text',
admin: {
description: 'Slug of the email template (auto-populated from template relationship)',
readOnly: true,
},
},
{ {
name: 'to', name: 'to',
type: 'text', type: 'text',
@@ -206,7 +198,7 @@ const Emails: CollectionConfig = {
readOnly: true, readOnly: true,
}, },
filterOptions: ({ id }) => { filterOptions: ({ id }) => {
const emailId = resolveID(id) const emailId = resolveID({ id })
return { return {
'input.emailId': { 'input.emailId': {
equals: emailId ? String(emailId) : '', equals: emailId ? String(emailId) : '',
@@ -216,27 +208,6 @@ const Emails: CollectionConfig = {
}, },
], ],
hooks: { hooks: {
beforeChange: [
async ({ data, req }) => {
// Auto-populate templateSlug from template relationship
if (data.template) {
try {
const template = await req.payload.findByID({
collection: 'email-templates',
id: typeof data.template === 'string' ? data.template : data.template.id,
})
data.templateSlug = template.slug
} catch (error) {
// If template lookup fails, clear the slug
data.templateSlug = undefined
}
} else {
// Clear templateSlug if template is removed
data.templateSlug = undefined
}
return data
}
],
// Simple approach: Only use afterChange hook for job management // Simple approach: Only use afterChange hook for job management
// This avoids complex interaction between hooks and ensures document ID is always available // This avoids complex interaction between hooks and ensures document ID is always available
afterChange: [ afterChange: [

View File

@@ -1,5 +1,5 @@
import { Payload } from 'payload' import { Payload } from 'payload'
import { getMailing, renderTemplateWithId, parseAndValidateEmails, sanitizeFromName } from './utils/helpers.js' import { getMailing, renderTemplate, parseAndValidateEmails, sanitizeFromName } from './utils/helpers.js'
import { BaseEmailDocument } from './types/index.js' import { BaseEmailDocument } from './types/index.js'
import { processJobById } from './utils/emailProcessor.js' import { processJobById } from './utils/emailProcessor.js'
import { createContextLogger } from './utils/logger.js' import { createContextLogger } from './utils/logger.js'
@@ -50,8 +50,7 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
let emailData: Partial<TEmail> = { ...options.data } as Partial<TEmail> let emailData: Partial<TEmail> = { ...options.data } as Partial<TEmail>
if (options.template) { if (options.template) {
// Look up and render the template in a single operation to avoid duplicate lookups const { html, text, subject } = await renderTemplate(
const { html, text, subject, templateId } = await renderTemplateWithId(
payload, payload,
options.template.slug, options.template.slug,
options.template.variables || {} options.template.variables || {}
@@ -59,7 +58,6 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
emailData = { emailData = {
...emailData, ...emailData,
template: templateId,
subject, subject,
html, html,
text, text,

View File

@@ -1,10 +1,10 @@
import {CollectionSlug, EmailAdapter, Payload, SendEmailOptions} from 'payload' import { Payload } from 'payload'
import { Liquid } from 'liquidjs' import { Liquid } from 'liquidjs'
import { import {
MailingPluginConfig, MailingPluginConfig,
TemplateVariables, TemplateVariables,
MailingService as IMailingService, MailingService as IMailingService,
BaseEmailDocument, BaseEmailTemplateDocument BaseEmail, BaseEmailTemplate, BaseEmailDocument, BaseEmailTemplateDocument
} from '../types/index.js' } from '../types/index.js'
import { serializeRichTextToHTML, serializeRichTextToText } from '../utils/richTextSerializer.js' import { serializeRichTextToHTML, serializeRichTextToText } from '../utils/richTextSerializer.js'
import { sanitizeDisplayName } from '../utils/helpers.js' import { sanitizeDisplayName } from '../utils/helpers.js'
@@ -12,6 +12,7 @@ import { sanitizeDisplayName } from '../utils/helpers.js'
export class MailingService implements IMailingService { export class MailingService implements IMailingService {
public payload: Payload public payload: Payload
private config: MailingPluginConfig private config: MailingPluginConfig
private emailAdapter: any
private templatesCollection: string private templatesCollection: string
private emailsCollection: string private emailsCollection: string
private liquid: Liquid | null | false = null private liquid: Liquid | null | false = null
@@ -30,13 +31,14 @@ export class MailingService implements IMailingService {
if (!this.payload.email) { if (!this.payload.email) {
throw new Error('Payload email configuration is required. Please configure email in your Payload config.') throw new Error('Payload email configuration is required. Please configure email in your Payload config.')
} }
this.emailAdapter = this.payload.email
} }
private ensureInitialized(): void { private ensureInitialized(): void {
if (!this.payload || !this.payload.db) { if (!this.payload || !this.payload.db) {
throw new Error('MailingService payload not properly initialized') throw new Error('MailingService payload not properly initialized')
} }
if (!this.payload.email) { if (!this.emailAdapter) {
throw new Error('Email adapter not configured. Please ensure Payload has email configured.') throw new Error('Email adapter not configured. Please ensure Payload has email configured.')
} }
} }
@@ -125,17 +127,6 @@ export class MailingService implements IMailingService {
throw new Error(`Email template not found: ${templateSlug}`) throw new Error(`Email template not found: ${templateSlug}`)
} }
return this.renderTemplateDocument(template, variables)
}
/**
* Render a template document (for when you already have the template loaded)
* This avoids duplicate template lookups
* @internal
*/
async renderTemplateDocument(template: BaseEmailTemplateDocument, variables: TemplateVariables): Promise<{ html: string; text: string; subject: string }> {
this.ensureInitialized()
const emailContent = await this.renderEmailTemplate(template, variables) const emailContent = await this.renderEmailTemplate(template, variables)
const subject = await this.renderTemplateString(template.subject || '', variables) const subject = await this.renderTemplateString(template.subject || '', variables)
@@ -151,7 +142,7 @@ export class MailingService implements IMailingService {
const currentTime = new Date().toISOString() const currentTime = new Date().toISOString()
const { docs: pendingEmails } = await this.payload.find({ const { docs: pendingEmails } = await this.payload.find({
collection: this.emailsCollection as CollectionSlug, collection: this.emailsCollection as any,
where: { where: {
and: [ and: [
{ {
@@ -191,7 +182,7 @@ export class MailingService implements IMailingService {
const retryTime = new Date(Date.now() - retryDelay).toISOString() const retryTime = new Date(Date.now() - retryDelay).toISOString()
const { docs: failedEmails } = await this.payload.find({ const { docs: failedEmails } = await this.payload.find({
collection: this.emailsCollection as CollectionSlug, collection: this.emailsCollection as any,
where: { where: {
and: [ and: [
{ {
@@ -231,7 +222,7 @@ export class MailingService implements IMailingService {
async processEmailItem(emailId: string): Promise<void> { async processEmailItem(emailId: string): Promise<void> {
try { try {
await this.payload.update({ await this.payload.update({
collection: this.emailsCollection as CollectionSlug, collection: this.emailsCollection as any,
id: emailId, id: emailId,
data: { data: {
status: 'processing', status: 'processing',
@@ -240,7 +231,7 @@ export class MailingService implements IMailingService {
}) })
const email = await this.payload.findByID({ const email = await this.payload.findByID({
collection: this.emailsCollection as CollectionSlug, collection: this.emailsCollection as any,
id: emailId, id: emailId,
depth: 1, depth: 1,
}) as BaseEmailDocument }) as BaseEmailDocument
@@ -253,7 +244,7 @@ export class MailingService implements IMailingService {
fromField = this.getDefaultFrom() fromField = this.getDefaultFrom()
} }
let mailOptions: SendEmailOptions = { let mailOptions: any = {
from: fromField, from: fromField,
to: email.to, to: email.to,
cc: email.cc || undefined, cc: email.cc || undefined,
@@ -264,19 +255,6 @@ export class MailingService implements IMailingService {
text: email.text || undefined, text: email.text || undefined,
} }
if (!mailOptions.from) {
throw new Error('Email from field is required')
}
if (!mailOptions.to || (Array.isArray(mailOptions.to) && mailOptions.to.length === 0)) {
throw new Error('Email to field is required')
}
if (!mailOptions.subject) {
throw new Error('Email subject is required')
}
if (!mailOptions.html && !mailOptions.text) {
throw new Error('Email content is required')
}
// Call beforeSend hook if configured // Call beforeSend hook if configured
if (this.config.beforeSend) { if (this.config.beforeSend) {
try { try {
@@ -302,10 +280,10 @@ export class MailingService implements IMailingService {
} }
// Send email using Payload's email adapter // Send email using Payload's email adapter
await this.payload.email.sendEmail(mailOptions) await this.emailAdapter.sendEmail(mailOptions)
await this.payload.update({ await this.payload.update({
collection: this.emailsCollection as CollectionSlug, collection: this.emailsCollection as any,
id: emailId, id: emailId,
data: { data: {
status: 'sent', status: 'sent',
@@ -319,7 +297,7 @@ export class MailingService implements IMailingService {
const maxAttempts = this.config.retryAttempts || 3 const maxAttempts = this.config.retryAttempts || 3
await this.payload.update({ await this.payload.update({
collection: this.emailsCollection as CollectionSlug, collection: this.emailsCollection as any,
id: emailId, id: emailId,
data: { data: {
status: attempts >= maxAttempts ? 'failed' : 'pending', status: attempts >= maxAttempts ? 'failed' : 'pending',
@@ -336,14 +314,14 @@ export class MailingService implements IMailingService {
private async incrementAttempts(emailId: string): Promise<number> { private async incrementAttempts(emailId: string): Promise<number> {
const email = await this.payload.findByID({ const email = await this.payload.findByID({
collection: this.emailsCollection as CollectionSlug, collection: this.emailsCollection as any,
id: emailId, id: emailId,
}) })
const newAttempts = ((email as any).attempts || 0) + 1 const newAttempts = ((email as any).attempts || 0) + 1
await this.payload.update({ await this.payload.update({
collection: this.emailsCollection as CollectionSlug, collection: this.emailsCollection as any,
id: emailId, id: emailId,
data: { data: {
attempts: newAttempts, attempts: newAttempts,

View File

@@ -1,4 +1,4 @@
import {Payload, SendEmailOptions} from 'payload' import { Payload } from 'payload'
import type { CollectionConfig, RichTextField } from 'payload' import type { CollectionConfig, RichTextField } from 'payload'
// Payload ID type (string or number) // Payload ID type (string or number)
@@ -14,7 +14,6 @@ export type JSONValue = string | number | boolean | { [k: string]: unknown } | u
export interface BaseEmailDocument { export interface BaseEmailDocument {
id: string | number id: string | number
template?: any template?: any
templateSlug?: string | null
to: string[] to: string[]
cc?: string[] | null cc?: string[] | null
bcc?: string[] | null bcc?: string[] | null
@@ -46,11 +45,28 @@ export interface BaseEmailTemplateDocument {
updatedAt?: string | Date | null updatedAt?: string | Date | null
} }
export type BaseEmail<TEmail extends BaseEmailDocument = BaseEmailDocument, TEmailTemplate extends BaseEmailTemplateDocument = BaseEmailTemplateDocument> = Omit<TEmail, 'id' | 'template'> & {template: Omit<TEmailTemplate, 'id'> | TEmailTemplate['id'] | undefined | null}
export type BaseEmailTemplate<TEmailTemplate extends BaseEmailTemplateDocument = BaseEmailTemplateDocument> = Omit<TEmailTemplate, 'id'>
export type TemplateRendererHook = (template: string, variables: Record<string, any>) => string | Promise<string> export type TemplateRendererHook = (template: string, variables: Record<string, any>) => string | Promise<string>
export type TemplateEngine = 'liquidjs' | 'mustache' | 'simple' export type TemplateEngine = 'liquidjs' | 'mustache' | 'simple'
export type BeforeSendHook = (options: SendEmailOptions, email: BaseEmailDocument) => SendEmailOptions | Promise<SendEmailOptions> export interface BeforeSendMailOptions {
from: string
to: string[]
cc?: string[]
bcc?: string[]
replyTo?: string
subject: string
html: string
text?: string
attachments?: any[]
[key: string]: any
}
export type BeforeSendHook = (options: BeforeSendMailOptions, email: BaseEmailDocument) => BeforeSendMailOptions | Promise<BeforeSendMailOptions>
export interface JobPollingConfig { export interface JobPollingConfig {
maxAttempts?: number // Maximum number of polling attempts (default: 5) maxAttempts?: number // Maximum number of polling attempts (default: 5)

View File

@@ -130,53 +130,6 @@ export const renderTemplate = async (payload: Payload, templateSlug: string, var
return mailing.service.renderTemplate(templateSlug, variables) return mailing.service.renderTemplate(templateSlug, variables)
} }
/**
* Render a template and return both rendered content and template ID
* This is used by sendEmail to avoid duplicate template lookups
* @internal
*/
export const renderTemplateWithId = async (
payload: Payload,
templateSlug: string,
variables: TemplateVariables
): Promise<{ html: string; text: string; subject: string; templateId: PayloadID }> => {
const mailing = getMailing(payload)
const templatesCollection = mailing.config.collections?.templates || 'email-templates'
// Runtime validation: Ensure the collection exists in Payload
if (!payload.collections[templatesCollection]) {
throw new Error(
`Templates collection '${templatesCollection}' not found. ` +
`Available collections: ${Object.keys(payload.collections).join(', ')}`
)
}
// Look up the template document once
const { docs: templateDocs } = await payload.find({
collection: templatesCollection as any,
where: {
slug: {
equals: templateSlug,
},
},
limit: 1,
})
if (!templateDocs || templateDocs.length === 0) {
throw new Error(`Template not found: ${templateSlug}`)
}
const templateDoc = templateDocs[0]
// Render using the document directly to avoid duplicate lookup
const rendered = await mailing.service.renderTemplateDocument(templateDoc, variables)
return {
...rendered,
templateId: templateDoc.id,
}
}
export const processEmails = async (payload: Payload): Promise<void> => { export const processEmails = async (payload: Payload): Promise<void> => {
const mailing = getMailing(payload) const mailing = getMailing(payload)
return mailing.service.processEmails() return mailing.service.processEmails()

View File

@@ -129,11 +129,7 @@ export async function updateEmailJobRelationship(
id: normalizedEmailId, id: normalizedEmailId,
}) })
// Extract IDs from job objects or use the value directly if it's already an ID const currentJobs = (currentEmail.jobs || []).map((job: any) => String(job))
// Jobs can be populated (objects with id field) or just IDs (strings/numbers)
const currentJobs = (currentEmail.jobs || []).map((job: any) =>
typeof job === 'object' && job !== null && job.id ? String(job.id) : String(job)
)
const allJobs = [...new Set([...currentJobs, ...normalizedJobIds])] // Deduplicate with normalized strings const allJobs = [...new Set([...currentJobs, ...normalizedJobIds])] // Deduplicate with normalized strings
await payload.update({ await payload.update({