knadh/listmonk · error

error compiling message

Error message

error compiling message: %v

What it means

Compilation of an individual message body from the campaign template failed while preparing the campaign for sending.

Solutions

  1. Fix the invalid template expression reported in the wrapped error
  2. Escape literal braces using {{"{{"}}"
  3. Ensure functions used are in the func map f registered with the template
  4. Use previewTemplate to smoke-test the body before saving

Example fix

// before
{{ .TrackLink | urlquery extraArg }}
// after
{{ .TrackLink | urlquery }}
Defensive patterns

Strategy: validation

Validate before calling

if _, err := template.New("msg").Funcs(funcMap).Parse(body); err != nil {
    return fmt.Errorf("invalid message template: %v", err)
}

Try / catch

if err := c.CompileTemplate(); err != nil {
    if strings.Contains(err.Error(), "error compiling message") {
        // surface the wrapped parse error to the content editor
    }
}

Prevention

When it happens

Trigger: CompileTemplate is called and template.New(ContentTpl).Funcs(f).Parse(body) fails because the campaign message body has invalid template actions or references unknown functions/fields.

Common situations: Same family as subject/base errors: stray "{{", misspelled function, wrong argument types to template funcs; often introduced after editing campaign content in the UI or via API import.

Related errors


AI-assisted analysis of knadh/listmonk@670c01717d (2026-09-01). Data as JSON: /api/errors/6815615feefc0606. Report an issue: GitHub.

Appendix: source

Thrown at models/campaigns.go:191

	// If the format is markdown, convert Markdown to HTML.
	if c.ContentType == CampaignContentTypeMarkdown {
		var b bytes.Buffer
		if err := markdown.Convert([]byte(c.Body), &b); err != nil {
			return err
		}
		body = b.String()
	} else {
		body = c.Body
	}

	// Compile the campaign message.
	for _, r := range regTplFuncs {
		body = r.regExp.ReplaceAllString(body, r.replace)
	}

	msgTpl, err := template.New(ContentTpl).Funcs(f).Parse(body)
	if err != nil {
		return fmt.Errorf("error compiling message: %v", err)
	}

	out, err := baseTPL.AddParseTree(ContentTpl, msgTpl.Tree)
	if err != nil {
		return fmt.Errorf("error inserting child template: %v", err)
	}
	c.Tpl = out

	if hasTplExpr(c.AltBody.String) {
		b := c.AltBody.String
		for _, r := range regTplFuncs {
			b = r.regExp.ReplaceAllString(b, r.replace)
		}
		bTpl, err := template.New(ContentTpl).Funcs(f).Parse(b)
		if err != nil {
			return fmt.Errorf("error compiling alt plaintext message: %v", err)
		}
		c.AltBodyTpl = bTpl

View on GitHub (pinned to 670c01717d)