mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 08:13:23 +00:00
Fix template relationship population in sendEmail and bump version to 0.4.18
The sendEmail function now properly populates the template relationship field when using template-based emails. This ensures: - Template relationship is set on the email document - templateSlug field is auto-populated via beforeChange hook - beforeSend hook has access to the full template relationship - Proper record of which template was used for each email 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -50,6 +50,22 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
||||
let emailData: Partial<TEmail> = { ...options.data } as Partial<TEmail>
|
||||
|
||||
if (options.template) {
|
||||
// Find the template document to get its ID for the relationship field
|
||||
const templatesCollection = mailingConfig.collections.templates || 'email-templates'
|
||||
const { docs: templateDocs } = await payload.find({
|
||||
collection: templatesCollection as any,
|
||||
where: {
|
||||
slug: {
|
||||
equals: options.template.slug,
|
||||
},
|
||||
},
|
||||
limit: 1,
|
||||
})
|
||||
|
||||
if (!templateDocs || templateDocs.length === 0) {
|
||||
throw new Error(`Template not found: ${options.template.slug}`)
|
||||
}
|
||||
|
||||
const { html, text, subject } = await renderTemplate(
|
||||
payload,
|
||||
options.template.slug,
|
||||
@@ -58,6 +74,7 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
||||
|
||||
emailData = {
|
||||
...emailData,
|
||||
template: templateDocs[0].id,
|
||||
subject,
|
||||
html,
|
||||
text,
|
||||
|
||||
Reference in New Issue
Block a user