mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 00:03:23 +00:00
Fix broken development routes and imports
Critical fixes: - Update dev/app/api/test-email/route.ts to use new sendEmail API instead of deprecated sendEmail/scheduleEmail - Fix dev/test-plugin.mjs imports to remove scheduleEmail reference - Update dev/README.md examples to use new sendEmail pattern - Replace templateId with template.slug throughout dev examples - Add support for direct HTML emails in test route The development routes now work correctly with v0.1.5 API changes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -184,25 +184,43 @@ The plugin automatically processes the outbox every 5 minutes and retries failed
|
|||||||
## Plugin API Usage
|
## Plugin API Usage
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
import { sendEmail, scheduleEmail } from '@xtr-dev/payload-mailing'
|
import { sendEmail } from '@xtr-dev/payload-mailing'
|
||||||
|
|
||||||
// Send immediate email
|
// Send immediate email with template
|
||||||
const emailId = await sendEmail(payload, {
|
const email = await sendEmail(payload, {
|
||||||
templateId: 'welcome-template-id',
|
template: {
|
||||||
to: 'user@example.com',
|
slug: 'welcome-email',
|
||||||
variables: {
|
variables: {
|
||||||
firstName: 'John',
|
firstName: 'John',
|
||||||
siteName: 'My App'
|
siteName: 'My App'
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
to: 'user@example.com',
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Schedule email
|
// Schedule email for later
|
||||||
const scheduledId = await scheduleEmail(payload, {
|
const scheduledEmail = await sendEmail(payload, {
|
||||||
templateId: 'reminder-template-id',
|
template: {
|
||||||
to: 'user@example.com',
|
slug: 'reminder',
|
||||||
scheduledAt: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours
|
|
||||||
variables: {
|
variables: {
|
||||||
eventName: 'Product Launch'
|
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: '<h1>Hello World</h1>',
|
||||||
|
text: 'Hello World'
|
||||||
|
}
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@@ -1,37 +1,50 @@
|
|||||||
import { getPayload } from 'payload'
|
import { getPayload } from 'payload'
|
||||||
import config from '@payload-config'
|
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) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const payload = await getPayload({ config })
|
const payload = await getPayload({ config })
|
||||||
const body = await request.json()
|
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
|
// Use the new sendEmail API
|
||||||
if (type === 'send') {
|
const emailOptions: any = {
|
||||||
// Send immediately
|
data: {
|
||||||
result = await sendEmail(payload, {
|
|
||||||
templateSlug,
|
|
||||||
to,
|
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({
|
return Response.json({
|
||||||
success: true,
|
success: true,
|
||||||
emailId: result,
|
emailId: result.id,
|
||||||
message: type === 'send' ? 'Email sent successfully' : 'Email scheduled successfully',
|
message: scheduledAt ? 'Email scheduled successfully' : 'Email queued successfully',
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Test email error:', error)
|
console.error('Test email error:', error)
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
// Simple test to verify plugin can be imported and initialized
|
// 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('✅ Plugin imports successfully')
|
||||||
console.log('✅ mailingPlugin:', typeof mailingPlugin)
|
console.log('✅ mailingPlugin:', typeof mailingPlugin)
|
||||||
console.log('✅ sendEmail:', typeof sendEmail)
|
console.log('✅ sendEmail:', typeof sendEmail)
|
||||||
console.log('✅ scheduleEmail:', typeof scheduleEmail)
|
console.log('✅ renderTemplate:', typeof renderTemplate)
|
||||||
|
|
||||||
// Test plugin configuration
|
// Test plugin configuration
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user