diff --git a/components/email/email-composer.tsx b/components/email/email-composer.tsx index 309f8c57..44a67ca5 100644 --- a/components/email/email-composer.tsx +++ b/components/email/email-composer.tsx @@ -30,7 +30,7 @@ import { useContactStore, getContactDisplayName, getContactPrimaryEmail } from " import { useTemplateStore } from "@/stores/template-store"; import { SubAddressHelper } from "@/components/identity/sub-address-helper"; import { generateSubAddress } from "@/lib/sub-addressing"; -import { substitutePlaceholders } from "@/lib/template-utils"; +import { substitutePlaceholders, spliceTemplateAboveSignature } from "@/lib/template-utils"; import { TemplatePicker } from "@/components/templates/template-picker"; import { TemplateForm } from "@/components/templates/template-form"; import type { EmailTemplate } from "@/lib/template-types"; @@ -1094,7 +1094,16 @@ export function EmailComposer({ if (mode === 'compose') { setSubject(filledSubject); - setBody(bodyContent); + // Compose bodies carry the embedded signature (see + // shouldEmbedSignatureInNewMail) and the send path assumes it stays + // there, so replace only the message content, not the signature block. + if (plainTextMode) { + setBody(shouldEmbedSignatureInNewMail + ? appendPlainTextSignature(bodyContent, signatureIdentity, { separator: signatureSeparatorEnabled }) + : bodyContent); + } else { + setBody((prev) => spliceTemplateAboveSignature(prev, bodyContent)); + } if (template.defaultRecipients?.to?.length) { setTo(template.defaultRecipients.to.map(parseRecipient)); } @@ -1115,7 +1124,7 @@ export function EmailComposer({ } setShowTemplatePicker(false); - }, [mode, plainTextMode]); + }, [mode, plainTextMode, shouldEmbedSignatureInNewMail, signatureIdentity, signatureSeparatorEnabled]); useEffect(() => { const handleTemplateKey = (e: KeyboardEvent) => { diff --git a/lib/__tests__/template-utils.test.ts b/lib/__tests__/template-utils.test.ts index 8ec0dd14..1a4fd8c4 100644 --- a/lib/__tests__/template-utils.test.ts +++ b/lib/__tests__/template-utils.test.ts @@ -10,6 +10,7 @@ import { filterTemplates, exportTemplates, importTemplates, + spliceTemplateAboveSignature, } from '../template-utils'; import type { EmailTemplate } from '../template-types'; @@ -333,3 +334,39 @@ describe('filterTemplates', () => { expect(filterTemplates(templates, 'xyz')).toHaveLength(0); }); }); + +describe('spliceTemplateAboveSignature', () => { + const template = '
Template body
'; + + it('keeps the signature block below the template (separator marker)', () => { + const prev = '--
Template body
--
old draft text
Template body
old draft text
', template)).toBe(template); + }); + + it('keeps everything from the start marker onward when the end marker is missing', () => { + const prev = 'old
--
Template body
--
half-written draft
--