diff --git a/CHANGELOG.md b/CHANGELOG.md index 861521fad..ea1de5550 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.48.1 - Unreleased +- Gmail: leave the account and every send-as alias out of `gmail send --reply-all` recipients, as `gmail reply-all` and `gmail drafts create --reply-all` already do. Only the sending address was excluded, so a reply-all to a message sent to one of your aliases copied that alias, and with `--from ` it copied your primary address. A reply-all whose only remaining recipients are in Cc is still sent, as `gmail reply-all` sends it. (#1318) — thanks @saariuslystoned. + ## 0.48.0 - 2026-11-05 **Highlights:** New backups stay local until you configure a remote; existing configurations keep working. Store service-account keys in the keyring, search Gmail IDs efficiently, and use environment-backed watch tokens, with Calendar fixes and security dependency updates. diff --git a/internal/cmd/gmail_reply.go b/internal/cmd/gmail_reply.go index 228b392d7..ed4632ea2 100644 --- a/internal/cmd/gmail_reply.go +++ b/internal/cmd/gmail_reply.go @@ -13,10 +13,11 @@ import ( "github.com/openclaw/gogcli/internal/mailmime" ) -// buildReplyAllRecipients constructs To and Cc lists for a reply-all. +// buildReplyAllRecipients constructs To and Cc lists for a reply-all, +// leaving out every one of selfEmails. // Per RFC 5322: if Reply-To header is present, use it instead of From. -func buildReplyAllRecipients(info *replyInfo, selfEmail string) (to, cc []string) { - recipients, err := buildReplyRecipients(info, []string{selfEmail}, true, nil, nil, nil, nil) +func buildReplyAllRecipients(info *replyInfo, selfEmails ...string) (to, cc []string) { + recipients, err := buildReplyRecipients(info, selfEmails, true, nil, nil, nil, nil) if err != nil { return nil, nil } diff --git a/internal/cmd/gmail_send.go b/internal/cmd/gmail_send.go index c59bbefc2..b2637eb2b 100644 --- a/internal/cmd/gmail_send.go +++ b/internal/cmd/gmail_send.go @@ -76,6 +76,8 @@ type sendMessageOptions struct { Attachments []mailmime.Attachment Track bool TrackingCfg *tracking.Config + // AllowMissingTo lets a message go out with only Cc or Bcc recipients. + AllowMissingTo bool } func (c *GmailSendCmd) Run(ctx context.Context, flags *RootFlags) error { @@ -173,7 +175,8 @@ func (c *GmailSendCmd) Run(ctx context.Context, flags *RootFlags) error { return err } - from, err := resolveComposeSender(ctx, svc, account, c.From) + sendAs, sendAsErr := listSendAs(ctx, svc) + from, err := resolveComposeFrom(ctx, svc, account, c.From, sendAs, sendAsErr) if err != nil { return err } @@ -194,7 +197,8 @@ func (c *GmailSendCmd) Run(ctx context.Context, flags *RootFlags) error { var toRecipients, ccRecipients []string if c.ReplyAll { // Auto-populate recipients from original message - toRecipients, ccRecipients = buildReplyAllRecipients(replyInfo, from.sendingEmail) + // Leave out the account and every send-as alias, as gmail reply-all does. + toRecipients, ccRecipients = buildReplyAllRecipients(replyInfo, selfEmailsForReply(account, from.sendingEmail, sendAs)...) } // Explicit --to and --cc override (not merge with) auto-populated @@ -209,8 +213,11 @@ func (c *GmailSendCmd) Run(ctx context.Context, flags *RootFlags) error { ccRecipients = explicitCc } - // Final validation: we must have at least one recipient - if len(toRecipients) == 0 { + // Final validation: we must have at least one recipient. A reply-all may + // leave only Cc recipients once the user's own addresses are dropped, which + // gmail reply-all also sends. + allowMissingTo := c.ReplyAll && strings.TrimSpace(c.To) == "" && len(ccRecipients)+len(explicitBcc) > 0 + if len(toRecipients) == 0 && !allowMissingTo { return usage("no recipients: specify --to or use --reply-all with a message that has recipients") } @@ -239,6 +246,8 @@ func (c *GmailSendCmd) Run(ctx context.Context, flags *RootFlags) error { Attachments: atts, Track: c.Track, TrackingCfg: trackingCfg, + + AllowMissingTo: allowMissingTo, }, batches) if err != nil { return err @@ -442,7 +451,7 @@ func sendGmailBatches(ctx context.Context, svc *gmail.Service, opts sendMessageO messageOpts := opts messageOpts.BodyHTML = htmlBody - msg, err := buildGmailMessage(ctx, messageOpts, batch, false) + msg, err := buildGmailMessage(ctx, messageOpts, batch, opts.AllowMissingTo) if err != nil { return nil, err } diff --git a/internal/cmd/gmail_send_reply_all_aliases_test.go b/internal/cmd/gmail_send_reply_all_aliases_test.go new file mode 100644 index 000000000..1565d68ae --- /dev/null +++ b/internal/cmd/gmail_send_reply_all_aliases_test.go @@ -0,0 +1,118 @@ +package cmd + +import ( + "encoding/base64" + "encoding/json" + "io" + "net/http" + "strings" + "testing" + + "google.golang.org/api/gmail/v1" +) + +// headerLine returns the first raw header line that starts with name. +func headerLine(raw, name string) string { + for _, line := range strings.Split(raw, "\n") { + if strings.HasPrefix(line, name+":") { + return strings.TrimSpace(line) + } + } + return "" +} + +// The original is addressed to the user's send-as alias and to bob. A +// reply-all must leave every address of the user out of the recipients, +// whichever command sends it. +func sendReplyAllViaCommand(t *testing.T, args ...string) string { + t.Helper() + return sendReplyAllWithHeaders(t, "alice@example.com", "me@example.com, alias@example.com, bob@example.com", "", args...) +} + +func sendReplyAllWithHeaders(t *testing.T, from, to, cc string, args ...string) string { + t.Helper() + var sentRaw string + svc, cleanup := newGmailServiceForTest(t, func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch { + case r.Method == http.MethodGet && r.URL.Path == "/gmail/v1/users/me/settings/sendAs": + _ = json.NewEncoder(w).Encode(map[string]any{"sendAs": []map[string]any{ + {"sendAsEmail": "me@example.com", "isPrimary": true, "verificationStatus": "accepted"}, + {"sendAsEmail": "alias@example.com", "verificationStatus": "accepted"}, + }}) + case r.Method == http.MethodGet && r.URL.Path == "/gmail/v1/users/me/messages/msg-1": + _ = json.NewEncoder(w).Encode(map[string]any{ + "id": "msg-1", "threadId": "thread-1", + "payload": map[string]any{"headers": []map[string]any{ + {"name": "Message-ID", "value": ""}, + {"name": "From", "value": from}, + {"name": "To", "value": to}, + {"name": "Cc", "value": cc}, + {"name": "Subject", "value": "Plans"}, + }}, + }) + case r.Method == http.MethodPost && r.URL.Path == "/gmail/v1/users/me/messages/send": + body, _ := io.ReadAll(r.Body) + var msg gmail.Message + _ = json.Unmarshal(body, &msg) + raw, _ := base64.RawURLEncoding.DecodeString(msg.Raw) + sentRaw = string(raw) + _ = json.NewEncoder(w).Encode(map[string]any{"id": "sent-1", "threadId": "thread-1"}) + default: + http.NotFound(w, r) + } + }) + defer cleanup() + + result := executeWithGmailTestService(t, append([]string{"--json", "--account", "me@example.com"}, args...), svc) + if result.err != nil { + t.Fatalf("Execute %v: %v", args, result.err) + } + return sentRaw +} + +func TestGmailSendReplyAllExcludesSendAsAliases(t *testing.T) { + for _, tc := range []struct { + name string + args []string + }{ + {name: "gmail reply-all (control)", args: []string{"gmail", "reply-all", "msg-1", "--body", "ok", "--no-quote"}}, + {name: "gmail send --reply-all", args: []string{"gmail", "send", "--reply-to-message-id", "msg-1", "--reply-all", "--body", "ok"}}, + {name: "gmail send --reply-all --from alias", args: []string{"gmail", "send", "--reply-to-message-id", "msg-1", "--reply-all", "--from", "alias@example.com", "--body", "ok"}}, + } { + t.Run(tc.name, func(t *testing.T) { + raw := sendReplyAllViaCommand(t, tc.args...) + recipients := headerLine(raw, "To") + " " + headerLine(raw, "Cc") + if !strings.Contains(recipients, "alice@example.com") || !strings.Contains(recipients, "bob@example.com") { + t.Fatalf("recipients %q, want alice and bob", recipients) + } + if strings.Contains(recipients, "alias@example.com") || strings.Contains(recipients, "me@example.com") { + t.Fatalf("recipients %q include the sender's own address", recipients) + } + }) + } +} + +// A message the user sent from an alias to their own primary address, copying +// bob: once both self addresses are left out, bob remains only in Cc. That is +// still a valid reply, as gmail reply-all already treats it. +func TestGmailSendReplyAllKeepsCcOnlyRecipients(t *testing.T) { + for _, tc := range []struct { + name string + args []string + }{ + {name: "gmail reply-all (control)", args: []string{"gmail", "reply-all", "msg-1", "--body", "ok", "--no-quote"}}, + {name: "gmail send --reply-all", args: []string{"gmail", "send", "--reply-to-message-id", "msg-1", "--reply-all", "--body", "ok"}}, + } { + t.Run(tc.name, func(t *testing.T) { + raw := sendReplyAllWithHeaders(t, "alias@example.com", "me@example.com", "bob@example.com", tc.args...) + if got := headerLine(raw, "Cc"); !strings.Contains(got, "bob@example.com") { + t.Fatalf("Cc %q, want bob", got) + } + recipients := headerLine(raw, "To") + " " + headerLine(raw, "Cc") + if strings.Contains(recipients, "alias@example.com") || strings.Contains(recipients, "me@example.com") { + t.Fatalf("recipients %q include the sender's own address", recipients) + } + }) + } +}