{"record":{"id":"65dd304e9f86382d","repo":"knadh/listmonk","slug":"error-compiling-subject-v-65dd30","errorCode":null,"errorMessage":"error compiling subject: %v","messagePattern":"error compiling subject: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"models/messages.go","lineNumber":115,"sourceCode":"\t\t}\n\t\tif err := t.ExecuteTemplate(&b, BaseTpl, data); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tm.AltBody = b.String()\n\t\tb.Reset()\n\t}\n\n\t// Was a subject provided in the message?\n\tvar (\n\t\tsubjTpl *txttpl.Template\n\t\tsubject = m.Subject\n\t)\n\tif subject != \"\" {\n\t\tif hasTplExpr(m.Subject) {\n\t\t\t// If the subject has a template string, render that.\n\t\t\ts, err := txttpl.New(BaseTpl).Funcs(funcs).Parse(m.Subject)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"error compiling subject: %v\", err)\n\t\t\t}\n\t\t\tsubjTpl = s\n\t\t}\n\t} else {\n\t\t// Use the subject from the template.\n\t\tsubject = tpl.Subject\n\t\tsubjTpl = tpl.SubjectTpl\n\t}\n\n\t// If the subject is also a template, render that.\n\tif subjTpl != nil {\n\t\tif err := subjTpl.ExecuteTemplate(&b, BaseTpl, data); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tm.Subject = b.String()\n\t\tb.Reset()\n\t} else {\n\t\tm.Subject = subject","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/knadh/listmonk/blob/670c01717d48647093335cc23a6be6f4b79c3b6b/models/messages.go#L97-L133","documentation":"This error is raised by TxMessage.Render in models/messages.go when the message's Subject contains template expressions ({{ ... }}) that fail to parse as a Go text/template. Since the subject is explicitly set on the TxMessage and hasTplExpr detects template strings, Render compiles it with txttpl.New(BaseTpl).Funcs(funcs).Parse before execution; any parse failure is wrapped as \"error compiling subject: %v\". It is a template syntax/compile failure, not a data-rendering failure.","triggerScenarios":"Calling SendTxMessage (or previewTemplate) with TxMessage.Subject non-empty and containing invalid template syntax — e.g. \"Receipt for {{ .Subscriber.Name \" (unclosed action), \"{{ .Tx.Amount | currencyee }}\" (unknown function in the FuncMap), or unbalanced braces like \"Order {{{ .Tx.ID }}\".","commonSituations":"Typo'd function names in subject lines, pasting HTML-template syntax into a text/template subject, hand-edited subject lines via API that break brace pairing, missing custom template funcs after a version change of the mailing library.","solutions":["Fix the template syntax in TxMessage.Subject per the wrapped error (unbalanced braces, bad function name, missing end tag).","Ensure all functions referenced in the subject exist in the FuncMap passed to Render.","If the subject needs no templating, remove all {{ }} expressions — it will then be used literally without compilation.","Test the subject string with text/template.Parse standalone to reproduce the exact parse error.","Review recent edits to the subject line via the admin UI/API for accidental brace corruption."],"exampleFix":"// before\nm.Subject = \"Order {{ .Tx.ID } confirmation\"\n// after\nm.Subject = \"Order {{ .Tx.ID }} confirmation\"","handlingStrategy":"validation","validationCode":"import (\"strings\" \"text/template\")\n\nfunc validateSubject(subject string, funcs template.FuncMap) error {\n\tif subject == \"\" || !strings.Contains(subject, \"{{\") {\n\t\treturn nil // literal subject; Render won't compile it\n\t}\n\t_, err := template.New(\"subject\").Funcs(funcs).Parse(subject)\n\treturn err\n}","typeGuard":"func hasTemplateExpr(s string) bool {\n\treturn strings.Contains(s, \"{{\") && strings.Contains(s, \"}}\")\n}","tryCatchPattern":"if err := msg.Render(sub, tpl, funcs); err != nil {\n\tif strings.HasPrefix(err.Error(), \"error compiling subject\") {\n\t\tlog.Errorf(\"subject template syntax invalid: %v\", err)\n\t\tmsg.Subject = fallbackStaticSubject // or surface the error to the API caller\n\t\treturn err\n\t}\n\treturn err\n}","preventionTips":["Parse-check any subject containing {{ }} before calling SendTxMessage.","Avoid nested/complex pipelines in subject lines — keep them to simple field lookups.","Only reference functions present in the FuncMap given to Render.","Reject or sanitize user-supplied subjects at the API boundary with a template parse check.","Keep a golden set of test subjects parsed in unit tests to catch FuncMap regressions."],"tags":["go","text-template","template-syntax","compilation"],"backgroundTag":"template-syntax-error","analyzedSha":"670c01717d48647093335cc23a6be6f4b79c3b6b","analyzedAt":"2026-09-01T03:39:35.452Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}