mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 08:13:23 +00:00
- Extract sendEmail function to src/sendEmail.ts as primary module - Export BaseEmailData and SendEmailOptions interfaces alongside - Update all imports to use new location - Add sendEmailDefault export for CommonJS compatibility - Export parseAndValidateEmails for external utility use - Updated README to highlight sendEmail as primary export Breaking change: BaseEmailData and SendEmailOptions now imported from main module, not utils/helpers 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
178 lines
4.7 KiB
TypeScript
178 lines
4.7 KiB
TypeScript
import { sendEmail, type BaseEmailData } from '../sendEmail.js'
|
|
|
|
export interface SendEmailTaskInput {
|
|
// Template mode fields
|
|
templateSlug?: string
|
|
variables?: Record<string, any>
|
|
|
|
// Direct email mode fields
|
|
subject?: string
|
|
html?: string
|
|
text?: string
|
|
|
|
// Common fields
|
|
to: string | string[]
|
|
cc?: string | string[]
|
|
bcc?: string | string[]
|
|
scheduledAt?: string // ISO date string
|
|
priority?: number
|
|
|
|
// Allow any additional fields that users might have in their email collection
|
|
[key: string]: any
|
|
}
|
|
|
|
export const sendEmailJob = {
|
|
slug: 'send-email',
|
|
label: 'Send Email',
|
|
inputSchema: [
|
|
{
|
|
name: 'templateSlug',
|
|
type: 'text' as const,
|
|
label: 'Template Slug',
|
|
admin: {
|
|
description: 'Use a template (leave empty for direct email)',
|
|
condition: (data: any) => !data.subject && !data.html
|
|
}
|
|
},
|
|
{
|
|
name: 'variables',
|
|
type: 'json' as const,
|
|
label: 'Template Variables',
|
|
admin: {
|
|
description: 'JSON object with variables for template rendering',
|
|
condition: (data: any) => Boolean(data.templateSlug)
|
|
}
|
|
},
|
|
{
|
|
name: 'subject',
|
|
type: 'text' as const,
|
|
label: 'Subject',
|
|
admin: {
|
|
description: 'Email subject (required if not using template)',
|
|
condition: (data: any) => !data.templateSlug
|
|
}
|
|
},
|
|
{
|
|
name: 'html',
|
|
type: 'textarea' as const,
|
|
label: 'HTML Content',
|
|
admin: {
|
|
description: 'HTML email content (required if not using template)',
|
|
condition: (data: any) => !data.templateSlug
|
|
}
|
|
},
|
|
{
|
|
name: 'text',
|
|
type: 'textarea' as const,
|
|
label: 'Text Content',
|
|
admin: {
|
|
description: 'Plain text email content (optional)',
|
|
condition: (data: any) => !data.templateSlug
|
|
}
|
|
},
|
|
{
|
|
name: 'to',
|
|
type: 'text' as const,
|
|
required: true,
|
|
label: 'To (Email Recipients)',
|
|
admin: {
|
|
description: 'Comma-separated list of email addresses'
|
|
}
|
|
},
|
|
{
|
|
name: 'cc',
|
|
type: 'text' as const,
|
|
label: 'CC (Carbon Copy)',
|
|
admin: {
|
|
description: 'Optional comma-separated list of CC email addresses'
|
|
}
|
|
},
|
|
{
|
|
name: 'bcc',
|
|
type: 'text' as const,
|
|
label: 'BCC (Blind Carbon Copy)',
|
|
admin: {
|
|
description: 'Optional comma-separated list of BCC email addresses'
|
|
}
|
|
},
|
|
{
|
|
name: 'scheduledAt',
|
|
type: 'date' as const,
|
|
label: 'Schedule For',
|
|
admin: {
|
|
description: 'Optional date/time to schedule email for future delivery'
|
|
}
|
|
},
|
|
{
|
|
name: 'priority',
|
|
type: 'number' as const,
|
|
label: 'Priority',
|
|
min: 1,
|
|
max: 10,
|
|
defaultValue: 5,
|
|
admin: {
|
|
description: 'Email priority (1 = highest, 10 = lowest)'
|
|
}
|
|
}
|
|
],
|
|
handler: async ({ input, payload }: any) => {
|
|
// Cast input to our expected type
|
|
const taskInput = input as SendEmailTaskInput
|
|
|
|
try {
|
|
// Prepare options for sendEmail based on task input
|
|
const sendEmailOptions: any = {
|
|
data: {}
|
|
}
|
|
|
|
// If using template mode
|
|
if (taskInput.templateSlug) {
|
|
sendEmailOptions.template = {
|
|
slug: taskInput.templateSlug,
|
|
variables: taskInput.variables || {}
|
|
}
|
|
}
|
|
|
|
// Build data object from task input
|
|
const dataFields = ['to', 'cc', 'bcc', 'subject', 'html', 'text', 'scheduledAt', 'priority']
|
|
const additionalFields: string[] = []
|
|
|
|
// Copy standard fields
|
|
dataFields.forEach(field => {
|
|
if (taskInput[field] !== undefined) {
|
|
sendEmailOptions.data[field] = taskInput[field]
|
|
}
|
|
})
|
|
|
|
// Copy any additional custom fields
|
|
Object.keys(taskInput).forEach(key => {
|
|
if (!['templateSlug', 'variables', ...dataFields].includes(key)) {
|
|
sendEmailOptions.data[key] = taskInput[key]
|
|
additionalFields.push(key)
|
|
}
|
|
})
|
|
|
|
// Use the sendEmail helper to create the email
|
|
const email = await sendEmail<BaseEmailData>(payload, sendEmailOptions)
|
|
|
|
return {
|
|
output: {
|
|
success: true,
|
|
emailId: email.id,
|
|
message: `Email queued successfully with ID: ${email.id}`,
|
|
mode: taskInput.templateSlug ? 'template' : 'direct',
|
|
templateSlug: taskInput.templateSlug || null,
|
|
subject: email.subject,
|
|
recipients: Array.isArray(email.to) ? email.to.length : 1,
|
|
scheduledAt: email.scheduledAt || null
|
|
}
|
|
}
|
|
|
|
} catch (error) {
|
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
throw new Error(`Failed to queue email: ${errorMessage}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
export default sendEmailJob |