mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 16:23:23 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0fa10164bf | |||
| ada13d27cb | |||
| de57dd4102 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/payload-mailing",
|
"name": "@xtr-dev/payload-mailing",
|
||||||
"version": "0.4.19",
|
"version": "0.4.21",
|
||||||
"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",
|
||||||
|
|||||||
@@ -206,7 +206,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) : '',
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { Payload } from 'payload'
|
import {CollectionSlug, EmailAdapter, Payload, SendEmailOptions} from 'payload'
|
||||||
import { Liquid } from 'liquidjs'
|
import { Liquid } from 'liquidjs'
|
||||||
import {
|
import {
|
||||||
MailingPluginConfig,
|
MailingPluginConfig,
|
||||||
TemplateVariables,
|
TemplateVariables,
|
||||||
MailingService as IMailingService,
|
MailingService as IMailingService,
|
||||||
BaseEmail, BaseEmailTemplate, BaseEmailDocument, BaseEmailTemplateDocument
|
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,7 +12,6 @@ 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
|
||||||
@@ -31,14 +30,13 @@ 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.emailAdapter) {
|
if (!this.payload.email) {
|
||||||
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.')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -153,7 +151,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 any,
|
collection: this.emailsCollection as CollectionSlug,
|
||||||
where: {
|
where: {
|
||||||
and: [
|
and: [
|
||||||
{
|
{
|
||||||
@@ -193,7 +191,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 any,
|
collection: this.emailsCollection as CollectionSlug,
|
||||||
where: {
|
where: {
|
||||||
and: [
|
and: [
|
||||||
{
|
{
|
||||||
@@ -233,7 +231,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 any,
|
collection: this.emailsCollection as CollectionSlug,
|
||||||
id: emailId,
|
id: emailId,
|
||||||
data: {
|
data: {
|
||||||
status: 'processing',
|
status: 'processing',
|
||||||
@@ -242,7 +240,7 @@ export class MailingService implements IMailingService {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const email = await this.payload.findByID({
|
const email = await this.payload.findByID({
|
||||||
collection: this.emailsCollection as any,
|
collection: this.emailsCollection as CollectionSlug,
|
||||||
id: emailId,
|
id: emailId,
|
||||||
depth: 1,
|
depth: 1,
|
||||||
}) as BaseEmailDocument
|
}) as BaseEmailDocument
|
||||||
@@ -255,7 +253,7 @@ export class MailingService implements IMailingService {
|
|||||||
fromField = this.getDefaultFrom()
|
fromField = this.getDefaultFrom()
|
||||||
}
|
}
|
||||||
|
|
||||||
let mailOptions: any = {
|
let mailOptions: SendEmailOptions = {
|
||||||
from: fromField,
|
from: fromField,
|
||||||
to: email.to,
|
to: email.to,
|
||||||
cc: email.cc || undefined,
|
cc: email.cc || undefined,
|
||||||
@@ -266,6 +264,19 @@ 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 {
|
||||||
@@ -291,10 +302,10 @@ export class MailingService implements IMailingService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send email using Payload's email adapter
|
// Send email using Payload's email adapter
|
||||||
await this.emailAdapter.sendEmail(mailOptions)
|
await this.payload.email.sendEmail(mailOptions)
|
||||||
|
|
||||||
await this.payload.update({
|
await this.payload.update({
|
||||||
collection: this.emailsCollection as any,
|
collection: this.emailsCollection as CollectionSlug,
|
||||||
id: emailId,
|
id: emailId,
|
||||||
data: {
|
data: {
|
||||||
status: 'sent',
|
status: 'sent',
|
||||||
@@ -308,7 +319,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 any,
|
collection: this.emailsCollection as CollectionSlug,
|
||||||
id: emailId,
|
id: emailId,
|
||||||
data: {
|
data: {
|
||||||
status: attempts >= maxAttempts ? 'failed' : 'pending',
|
status: attempts >= maxAttempts ? 'failed' : 'pending',
|
||||||
@@ -325,14 +336,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 any,
|
collection: this.emailsCollection as CollectionSlug,
|
||||||
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 any,
|
collection: this.emailsCollection as CollectionSlug,
|
||||||
id: emailId,
|
id: emailId,
|
||||||
data: {
|
data: {
|
||||||
attempts: newAttempts,
|
attempts: newAttempts,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Payload } from 'payload'
|
import {Payload, SendEmailOptions} 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)
|
||||||
@@ -46,28 +46,11 @@ 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 interface BeforeSendMailOptions {
|
export type BeforeSendHook = (options: SendEmailOptions, email: BaseEmailDocument) => SendEmailOptions | Promise<SendEmailOptions>
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user