{"record":{"id":"00c7188f32d52e44","repo":"cloudreve/cloudreve","slug":"failed-to-execute-email-template-w","errorCode":null,"errorMessage":"failed to execute email template: %w","messagePattern":"failed to execute email template: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/email/template.go","lineNumber":60,"sourceCode":"\tif err != nil {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to parse email title: %w\", err)\n\t}\n\n\tvar resTitle strings.Builder\n\terr = tmplTitle.Execute(&resTitle, resetCtx)\n\tif err != nil {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to execute email title: %w\", err)\n\t}\n\n\ttmplBody, err := template.New(\"resetBody\").Parse(selected.Body)\n\tif err != nil {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to parse email template: %w\", err)\n\t}\n\n\tvar resBody strings.Builder\n\terr = tmplBody.Execute(&resBody, resetCtx)\n\tif err != nil {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to execute email template: %w\", err)\n\t}\n\n\treturn resTitle.String(), resBody.String(), nil\n}\n\n// ActivationContext used for variables in activation email\ntype ActivationContext struct {\n\t*CommonContext\n\tUser *ent.User\n\tUrl  string\n}\n\n// NewActivationEmail generates activation email from template\nfunc NewActivationEmail(ctx context.Context, settings setting.Provider, user *ent.User, url string) (string, string, error) {\n\ttemplates := settings.ActivationEmailTemplate(ctx)\n\tif len(templates) == 0 {\n\t\treturn \"\", \"\", fmt.Errorf(\"activation email template not configured\")\n\t}","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/cloudreve/cloudreve/blob/20c95ad73f3a8bcb72887fea91ff31ab24fa1011/pkg/email/template.go#L42-L78","documentation":"The reset-email body parsed but executing it against ResetContext failed. Execution errors come from unknown fields/methods on the context, nil dereferences, or functions returning errors while rendering the (usually HTML) body.","triggerScenarios":"Body references {{.SiteUrl.Title}} although SiteUrl is a plain string, {{.User.Name}} with a nil User, or any field not present on ResetContext/CommonContext; a range over a non-collection field.","commonSituations":"Rich HTML templates using variables documented for a different version or a different email type; templates assuming nested settings fields that were flattened.","solutions":["Validate each variable in the body against ResetContext fields (SiteBasic, Logo, SiteUrl, User, Url) and the ent.User fields used","Fix or remove unknown fields; use {{if}} guards around optional ones","Test-render the body with a sample ResetContext in a unit test before saving to settings","After fixing, verify with a real password-reset request"],"exampleFix":"// before (body template)\n<p>Welcome to {{.SiteTitle}}, {{.UserName}}</p>\n// after\n<p>Welcome to {{.SiteBasic.Title}}, {{.User.Name}}</p>","handlingStrategy":"try-catch","validationCode":"func bodyExecutes(body string, sample ResetContext) error {\n    t, err := template.New(\"b\").Parse(body)\n    if err != nil {\n        return err\n    }\n    return t.Execute(io.Discard, sample)\n}","typeGuard":null,"tryCatchPattern":"_, _, err := email.NewResetEmail(ctx, settings, user, url)\nif err != nil && strings.Contains(err.Error(), \"failed to execute email template\") {\n    // Execute errors name the offending field: log it verbatim for the admin\n    log.Error(\"reset body template field error: %v\", err)\n}","preventionTips":["Dry-run saved bodies against a sample ResetContext in a settings self-test","Pin templates to documented context fields only","After product upgrades re-validate saved templates because context fields can change"],"tags":["go","template","email","runtime-error","configuration"],"backgroundTag":null,"analyzedSha":"20c95ad73f3a8bcb72887fea91ff31ab24fa1011","analyzedAt":"2026-08-16T01:42:55.403Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}