From f43178690703c9aa71c6ef8cafbd1c92383cc557 Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Mon, 6 Oct 2025 23:48:23 +0200 Subject: [PATCH] Fix template relationship population in sendEmail and bump version to 0.4.18 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- package.json | 2 +- src/sendEmail.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 52a6ea7..7409fb6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@xtr-dev/payload-mailing", - "version": "0.4.17", + "version": "0.4.18", "description": "Template-based email system with scheduling and job processing for PayloadCMS", "type": "module", "main": "dist/index.js", diff --git a/src/sendEmail.ts b/src/sendEmail.ts index 3b6d624..9768aad 100644 --- a/src/sendEmail.ts +++ b/src/sendEmail.ts @@ -50,6 +50,22 @@ export const sendEmail = async = { ...options.data } as Partial 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