{"record":{"id":"acd78e14388aa823","repo":"larksuite/cli","slug":"emlbuilder-at-least-one-recipient-to-cc-bcc-is","errorCode":null,"errorMessage":"emlbuilder: at least one recipient (To/CC/BCC) is required","messagePattern":"emlbuilder: at least one recipient \\(To/CC/BCC\\) is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shortcuts/mail/emlbuilder/builder.go","lineNumber":682,"sourceCode":"}\n\n// Build validates the builder and returns the raw EML bytes.\n//\n// Constraints (Lark API requirements):\n//   - From is mandatory.\n//   - At least one of To/CC/BCC must be set.\n//   - Line endings are LF (\\n), not CRLF.\n//   - Content-Type parameters are written on a single line (no header folding).\n//   - Non-ASCII body content is base64 (StdEncoding) encoded.\nfunc (b Builder) Build() ([]byte, error) {\n\tif b.err != nil {\n\t\treturn nil, b.err\n\t}\n\tif b.from.Address == \"\" {\n\t\treturn nil, fmt.Errorf(\"emlbuilder: From address is required\") //nolint:forbidigo // intermediate EML builder error; mail command layer wraps into typed ValidationError.\n\t}\n\tif !b.allowNoRecipients && len(b.to)+len(b.cc)+len(b.bcc) == 0 {\n\t\treturn nil, fmt.Errorf(\"emlbuilder: at least one recipient (To/CC/BCC) is required\") //nolint:forbidigo // intermediate EML builder error; mail command layer wraps into typed ValidationError.\n\t}\n\n\tdate := b.date\n\tif date.IsZero() {\n\t\tdate = time.Now()\n\t}\n\n\tmsgID := b.messageID\n\tif msgID == \"\" {\n\t\tmsgID = fmt.Sprintf(\"%d.%d@larksuite-cli\", date.UnixNano(), rand.Int63())\n\t}\n\n\tvar buf bytes.Buffer\n\n\t// ── Top-level headers ──────────────────────────────────────────────────────\n\t// Order follows common convention; Lark API does not require a specific order.\n\twriteHeader(&buf, \"Subject\", encodeHeaderValue(b.subject))\n\twriteHeader(&buf, \"From\", b.from.String())","sourceCodeStart":664,"sourceCodeEnd":700,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/shortcuts/mail/emlbuilder/builder.go#L664-L700","documentation":"Build() requires at least one recipient among To, CC, and BCC unless the builder was configured with AllowNoRecipients. An EML with no recipient headers is not deliverable, so the builder refuses to serialize it by default. Wrapped into a typed ValidationError by the mail command layer.","triggerScenarios":"Calling Build() with no To/CC/BCC added and allowNoRecipients false — e.g. all recipient lists empty because the recipients parameter was an empty slice or every address was filtered out.","commonSituations":"Recipient list filtered to zero entries (e.g. by dedupe or domain rules) before building; config-driven recipient list missing/blank; batch logic skipping the only recipient due to a validation rule.","solutions":["Add at least one recipient with b.To(...), b.CC(...), or b.BCC(...) before Build().","If the message genuinely needs no recipients (e.g. draft/template emission), enable the builder's allow-no-recipients option.","Validate the recipient list is non-empty at the application level before invoking the builder to give a clearer message."],"exampleFix":"// before\nb.From(\"me@x.com\")\nraw, err := b.Build() // error: at least one recipient required\n// after\nb.From(\"me@x.com\")\nb.To(\"dest@x.com\")\nraw, err := b.Build()","handlingStrategy":"validation","validationCode":"if len(recipients) == 0 {\n    return errors.New(\"no recipients after filtering; refusing to build EML\")\n}\nfor _, r := range recipients { b.To(r) }","typeGuard":null,"tryCatchPattern":"raw, err := b.Build()\nif err != nil {\n    var verr *ValidationError\n    if errors.As(err, &verr) && strings.Contains(err.Error(), \"at least one recipient\") {\n        return fmt.Errorf(\"recipient list empty: %w\", verr)\n    }\n    return err\n}","preventionTips":["Check the recipient slice is non-empty after any filtering/dedup step.","Log when a filter removes all recipients instead of silently building an empty message.","Only use the allow-no-recipients option for intentional draft/template flows."],"tags":["email","required-field","recipients","eml"],"backgroundTag":"missing-required-argument","analyzedSha":"7fd6ef3c07182257ce776cdc5a614e122d5bd4b3","analyzedAt":"2026-09-04T21:17:44.649Z","contentChangedAt":"2026-09-04T21:17:44.649Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}