diff --git a/dev/README.md b/dev/README.md index bcea813..5e7f8e2 100644 --- a/dev/README.md +++ b/dev/README.md @@ -184,25 +184,43 @@ The plugin automatically processes the outbox every 5 minutes and retries failed ## Plugin API Usage ```javascript -import { sendEmail, scheduleEmail } from '@xtr-dev/payload-mailing' +import { sendEmail } from '@xtr-dev/payload-mailing' -// Send immediate email -const emailId = await sendEmail(payload, { - templateId: 'welcome-template-id', - to: 'user@example.com', - variables: { - firstName: 'John', - siteName: 'My App' +// Send immediate email with template +const email = await sendEmail(payload, { + template: { + slug: 'welcome-email', + variables: { + firstName: 'John', + siteName: 'My App' + } + }, + data: { + to: 'user@example.com', } }) -// Schedule email -const scheduledId = await scheduleEmail(payload, { - templateId: 'reminder-template-id', - to: 'user@example.com', - scheduledAt: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours - variables: { - eventName: 'Product Launch' +// Schedule email for later +const scheduledEmail = await sendEmail(payload, { + template: { + slug: 'reminder', + variables: { + eventName: 'Product Launch' + } + }, + data: { + to: 'user@example.com', + scheduledAt: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours + } +}) + +// Send direct HTML email (no template) +const directEmail = await sendEmail(payload, { + data: { + to: 'user@example.com', + subject: 'Direct Email', + html: '

Hello World

', + text: 'Hello World' } }) ``` \ No newline at end of file diff --git a/dev/app/api/test-email/route.ts b/dev/app/api/test-email/route.ts index 3572ca4..bf16533 100644 --- a/dev/app/api/test-email/route.ts +++ b/dev/app/api/test-email/route.ts @@ -1,37 +1,50 @@ import { getPayload } from 'payload' import config from '@payload-config' -import { sendEmail, scheduleEmail } from '@xtr-dev/payload-mailing' +import { sendEmail } from '@xtr-dev/payload-mailing' export async function POST(request: Request) { try { const payload = await getPayload({ config }) const body = await request.json() - const { type = 'send', templateSlug, to, variables, scheduledAt } = body + const { type = 'send', templateSlug, to, variables, scheduledAt, subject, html, text } = body - let result - if (type === 'send') { - // Send immediately - result = await sendEmail(payload, { - templateSlug, + // Use the new sendEmail API + const emailOptions: any = { + data: { to, - variables, - }) - } else if (type === 'schedule') { - // Schedule for later - result = await scheduleEmail(payload, { - templateSlug, - to, - variables, - scheduledAt: scheduledAt ? new Date(scheduledAt) : new Date(Date.now() + 60000), // Default to 1 minute - }) - } else { - return Response.json({ error: 'Invalid type. Use "send" or "schedule"' }, { status: 400 }) + } } + // Add template if provided + if (templateSlug) { + emailOptions.template = { + slug: templateSlug, + variables: variables || {} + } + } else if (subject && html) { + // Direct email without template + emailOptions.data.subject = subject + emailOptions.data.html = html + if (text) { + emailOptions.data.text = text + } + } else { + return Response.json({ + error: 'Either templateSlug or subject+html must be provided' + }, { status: 400 }) + } + + // Add scheduling if needed + if (type === 'schedule' || scheduledAt) { + emailOptions.data.scheduledAt = scheduledAt ? new Date(scheduledAt) : new Date(Date.now() + 60000) + } + + const result = await sendEmail(payload, emailOptions) + return Response.json({ success: true, - emailId: result, - message: type === 'send' ? 'Email sent successfully' : 'Email scheduled successfully', + emailId: result.id, + message: scheduledAt ? 'Email scheduled successfully' : 'Email queued successfully', }) } catch (error) { console.error('Test email error:', error) diff --git a/dev/test-plugin.mjs b/dev/test-plugin.mjs index 6d7cf34..0fe1382 100644 --- a/dev/test-plugin.mjs +++ b/dev/test-plugin.mjs @@ -1,10 +1,10 @@ // Simple test to verify plugin can be imported and initialized -import { mailingPlugin, sendEmail, scheduleEmail } from '@xtr-dev/payload-mailing' +import { mailingPlugin, sendEmail, renderTemplate } from '@xtr-dev/payload-mailing' console.log('✅ Plugin imports successfully') console.log('✅ mailingPlugin:', typeof mailingPlugin) -console.log('✅ sendEmail:', typeof sendEmail) -console.log('✅ scheduleEmail:', typeof scheduleEmail) +console.log('✅ sendEmail:', typeof sendEmail) +console.log('✅ renderTemplate:', typeof renderTemplate) // Test plugin configuration try {