Skip to content

Commit

Permalink
compose: merge repeated address headers
Browse files Browse the repository at this point in the history
When composing a message with edit-headers=true and some To, Cc, Bcc
headers are repeated, only the last occurrence will be considered. All
other addresses will be dropped and lost without any error/warning.

When parsing the embedded headers, merge any repeated To, Cc, Bcc
headers and warn the user about it.

Reported-by: Inwit <[email protected]>
Signed-off-by: Robin Jarry <[email protected]>
Tested-by: Inwit <[email protected]>
  • Loading branch information
rjarry committed Jan 18, 2025
1 parent fae4a0a commit 5a90e2f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,30 @@ func (c *Composer) parseEmbeddedHeader() (*mail.Header, error) {
} else if err != nil {
return nil, fmt.Errorf("mail.ReadMessage: %w", err)
}

// merge repeated to, cc, and bcc headers into a single one of each
for _, key := range []string{"To", "Cc", "Bcc"} {
fields := msg.Header.FieldsByKey(key)
if fields.Len() <= 1 {
continue
}
var addrs []*mail.Address
for fields.Next() {
if strings.TrimSpace(fields.Value()) == "" {
continue
}
al, err := mail.ParseAddressList(fields.Value())
if err != nil {
return nil, fmt.Errorf(
"%s: cannot parse address list: %w", key, err)
}
addrs = append(addrs, al...)
}
msg.Header.SetAddressList(key, addrs)
PushWarning(fmt.Sprintf(
"Multiple %s headers found; merged in a single one.", key))
}

return &msg.Header, nil
}

Expand Down

0 comments on commit 5a90e2f

Please sign in to comment.