mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 08:13:23 +00:00
Compare commits
46 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e95296feff | ||
|
|
8406bca718 | ||
|
|
8e1128f1e8 | ||
|
|
50ce181893 | ||
|
|
2c0f202518 | ||
|
|
aa5a03b5b0 | ||
|
|
2220d83288 | ||
|
|
de1ae636de | ||
|
|
5e0ed0a03a | ||
|
|
060b1914b6 | ||
|
|
d82b3f2276 | ||
|
|
05f4cd0d7c | ||
|
|
bba223410d | ||
|
|
a40d87c63c | ||
|
|
fde8eb538d | ||
|
|
ff94d72d49 | ||
|
|
0083e8e1fa | ||
|
|
6cf055178b | ||
|
|
556d910e30 | ||
|
|
efdfaf5889 | ||
|
|
f12ac8172e | ||
|
|
672ab3236a | ||
|
|
7f04275d39 | ||
|
|
ea87f14308 | ||
|
|
ff788c1ecf | ||
|
|
72f3d7f66d | ||
|
|
5905f732de | ||
|
|
685875d1b9 | ||
|
|
768b70a003 | ||
|
|
21b22a033a | ||
|
|
03f1f62fbf | ||
|
|
e38b63d814 | ||
|
|
c78a8c2480 | ||
|
|
0c4d894f51 | ||
|
|
6d4e020133 | ||
|
|
b3de54b953 | ||
|
|
ed058c0721 | ||
|
|
273dea5a73 | ||
|
|
c81ef7f8a8 | ||
|
|
cddcfb1e4c | ||
|
|
80d32674a9 | ||
|
|
c7af628beb | ||
|
|
048fa33747 | ||
|
|
cb62874500 | ||
|
|
9efea193b1 | ||
|
|
c09d7d4fc5 |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"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",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
|
||||
@@ -12,7 +12,7 @@ const Emails: CollectionConfig = {
|
||||
description: 'Email delivery and status tracking',
|
||||
},
|
||||
defaultPopulate: {
|
||||
templateSlug: true,
|
||||
template: true,
|
||||
to: true,
|
||||
cc: true,
|
||||
bcc: true,
|
||||
@@ -40,14 +40,6 @@ const Emails: CollectionConfig = {
|
||||
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',
|
||||
type: 'text',
|
||||
@@ -206,7 +198,7 @@ const Emails: CollectionConfig = {
|
||||
readOnly: true,
|
||||
},
|
||||
filterOptions: ({ id }) => {
|
||||
const emailId = resolveID(id)
|
||||
const emailId = resolveID({ id })
|
||||
return {
|
||||
'input.emailId': {
|
||||
equals: emailId ? String(emailId) : '',
|
||||
@@ -216,27 +208,6 @@ const Emails: CollectionConfig = {
|
||||
},
|
||||
],
|
||||
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
|
||||
// This avoids complex interaction between hooks and ensures document ID is always available
|
||||
afterChange: [
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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 { processJobById } from './utils/emailProcessor.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>
|
||||
|
||||
if (options.template) {
|
||||
// Look up and render the template in a single operation to avoid duplicate lookups
|
||||
const { html, text, subject, templateId } = await renderTemplateWithId(
|
||||
const { html, text, subject } = await renderTemplate(
|
||||
payload,
|
||||
options.template.slug,
|
||||
options.template.variables || {}
|
||||
@@ -59,7 +58,6 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
||||
|
||||
emailData = {
|
||||
...emailData,
|
||||
template: templateId,
|
||||
subject,
|
||||
html,
|
||||
text,
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import {CollectionSlug, EmailAdapter, Payload, SendEmailOptions} from 'payload'
|
||||
import { Payload } from 'payload'
|
||||
import { Liquid } from 'liquidjs'
|
||||
import {
|
||||
MailingPluginConfig,
|
||||
TemplateVariables,
|
||||
MailingService as IMailingService,
|
||||
BaseEmailDocument, BaseEmailTemplateDocument
|
||||
BaseEmail, BaseEmailTemplate, BaseEmailDocument, BaseEmailTemplateDocument
|
||||
} from '../types/index.js'
|
||||
import { serializeRichTextToHTML, serializeRichTextToText } from '../utils/richTextSerializer.js'
|
||||
import { sanitizeDisplayName } from '../utils/helpers.js'
|
||||
@@ -12,6 +12,7 @@ import { sanitizeDisplayName } from '../utils/helpers.js'
|
||||
export class MailingService implements IMailingService {
|
||||
public payload: Payload
|
||||
private config: MailingPluginConfig
|
||||
private emailAdapter: any
|
||||
private templatesCollection: string
|
||||
private emailsCollection: string
|
||||
private liquid: Liquid | null | false = null
|
||||
@@ -30,13 +31,14 @@ export class MailingService implements IMailingService {
|
||||
if (!this.payload.email) {
|
||||
throw new Error('Payload email configuration is required. Please configure email in your Payload config.')
|
||||
}
|
||||
this.emailAdapter = this.payload.email
|
||||
}
|
||||
|
||||
private ensureInitialized(): void {
|
||||
if (!this.payload || !this.payload.db) {
|
||||
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.')
|
||||
}
|
||||
}
|
||||
@@ -125,17 +127,6 @@ export class MailingService implements IMailingService {
|
||||
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 subject = await this.renderTemplateString(template.subject || '', variables)
|
||||
|
||||
@@ -151,7 +142,7 @@ export class MailingService implements IMailingService {
|
||||
const currentTime = new Date().toISOString()
|
||||
|
||||
const { docs: pendingEmails } = await this.payload.find({
|
||||
collection: this.emailsCollection as CollectionSlug,
|
||||
collection: this.emailsCollection as any,
|
||||
where: {
|
||||
and: [
|
||||
{
|
||||
@@ -191,7 +182,7 @@ export class MailingService implements IMailingService {
|
||||
const retryTime = new Date(Date.now() - retryDelay).toISOString()
|
||||
|
||||
const { docs: failedEmails } = await this.payload.find({
|
||||
collection: this.emailsCollection as CollectionSlug,
|
||||
collection: this.emailsCollection as any,
|
||||
where: {
|
||||
and: [
|
||||
{
|
||||
@@ -231,7 +222,7 @@ export class MailingService implements IMailingService {
|
||||
async processEmailItem(emailId: string): Promise<void> {
|
||||
try {
|
||||
await this.payload.update({
|
||||
collection: this.emailsCollection as CollectionSlug,
|
||||
collection: this.emailsCollection as any,
|
||||
id: emailId,
|
||||
data: {
|
||||
status: 'processing',
|
||||
@@ -240,7 +231,7 @@ export class MailingService implements IMailingService {
|
||||
})
|
||||
|
||||
const email = await this.payload.findByID({
|
||||
collection: this.emailsCollection as CollectionSlug,
|
||||
collection: this.emailsCollection as any,
|
||||
id: emailId,
|
||||
depth: 1,
|
||||
}) as BaseEmailDocument
|
||||
@@ -253,7 +244,7 @@ export class MailingService implements IMailingService {
|
||||
fromField = this.getDefaultFrom()
|
||||
}
|
||||
|
||||
let mailOptions: SendEmailOptions = {
|
||||
let mailOptions: any = {
|
||||
from: fromField,
|
||||
to: email.to,
|
||||
cc: email.cc || undefined,
|
||||
@@ -264,19 +255,6 @@ export class MailingService implements IMailingService {
|
||||
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
|
||||
if (this.config.beforeSend) {
|
||||
try {
|
||||
@@ -302,10 +280,10 @@ export class MailingService implements IMailingService {
|
||||
}
|
||||
|
||||
// Send email using Payload's email adapter
|
||||
await this.payload.email.sendEmail(mailOptions)
|
||||
await this.emailAdapter.sendEmail(mailOptions)
|
||||
|
||||
await this.payload.update({
|
||||
collection: this.emailsCollection as CollectionSlug,
|
||||
collection: this.emailsCollection as any,
|
||||
id: emailId,
|
||||
data: {
|
||||
status: 'sent',
|
||||
@@ -319,7 +297,7 @@ export class MailingService implements IMailingService {
|
||||
const maxAttempts = this.config.retryAttempts || 3
|
||||
|
||||
await this.payload.update({
|
||||
collection: this.emailsCollection as CollectionSlug,
|
||||
collection: this.emailsCollection as any,
|
||||
id: emailId,
|
||||
data: {
|
||||
status: attempts >= maxAttempts ? 'failed' : 'pending',
|
||||
@@ -336,14 +314,14 @@ export class MailingService implements IMailingService {
|
||||
|
||||
private async incrementAttempts(emailId: string): Promise<number> {
|
||||
const email = await this.payload.findByID({
|
||||
collection: this.emailsCollection as CollectionSlug,
|
||||
collection: this.emailsCollection as any,
|
||||
id: emailId,
|
||||
})
|
||||
|
||||
const newAttempts = ((email as any).attempts || 0) + 1
|
||||
|
||||
await this.payload.update({
|
||||
collection: this.emailsCollection as CollectionSlug,
|
||||
collection: this.emailsCollection as any,
|
||||
id: emailId,
|
||||
data: {
|
||||
attempts: newAttempts,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {Payload, SendEmailOptions} from 'payload'
|
||||
import { Payload } from 'payload'
|
||||
import type { CollectionConfig, RichTextField } from 'payload'
|
||||
|
||||
// Payload ID type (string or number)
|
||||
@@ -14,7 +14,6 @@ export type JSONValue = string | number | boolean | { [k: string]: unknown } | u
|
||||
export interface BaseEmailDocument {
|
||||
id: string | number
|
||||
template?: any
|
||||
templateSlug?: string | null
|
||||
to: string[]
|
||||
cc?: string[] | null
|
||||
bcc?: string[] | null
|
||||
@@ -46,11 +45,28 @@ export interface BaseEmailTemplateDocument {
|
||||
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 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 {
|
||||
maxAttempts?: number // Maximum number of polling attempts (default: 5)
|
||||
|
||||
@@ -130,53 +130,6 @@ export const renderTemplate = async (payload: Payload, templateSlug: string, var
|
||||
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> => {
|
||||
const mailing = getMailing(payload)
|
||||
return mailing.service.processEmails()
|
||||
|
||||
@@ -129,11 +129,7 @@ export async function updateEmailJobRelationship(
|
||||
id: normalizedEmailId,
|
||||
})
|
||||
|
||||
// Extract IDs from job objects or use the value directly if it's already an ID
|
||||
// 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 currentJobs = (currentEmail.jobs || []).map((job: any) => String(job))
|
||||
const allJobs = [...new Set([...currentJobs, ...normalizedJobIds])] // Deduplicate with normalized strings
|
||||
|
||||
await payload.update({
|
||||
|
||||
Reference in New Issue
Block a user