Refine CcFromString to handle spaces and empty addresses.

This change ensures that email addresses with leading or trailing spaces are trimmed, and empty addresses resulting from multiple commas are ignored. This helps prevent potential errors with malformed email addresses in the "Cc" field.
This commit is contained in:
Winni Neessen 2024-10-26 18:21:50 +02:00
parent d7b32480fd
commit 855d7f0867
Signed by: wneessen
GPG key ID: 385AC9889632126E

11
msg.go
View file

@ -847,7 +847,16 @@ func (m *Msg) CcIgnoreInvalid(rcpts ...string) {
// References: // References:
// - https://datatracker.ietf.org/doc/html/rfc5322#section-3.6.3 // - https://datatracker.ietf.org/doc/html/rfc5322#section-3.6.3
func (m *Msg) CcFromString(rcpts string) error { func (m *Msg) CcFromString(rcpts string) error {
return m.Cc(strings.Split(rcpts, ",")...) src := strings.Split(rcpts, ",")
var dst []string
for _, address := range src {
address = strings.TrimSpace(address)
if address == "" {
continue
}
dst = append(dst, address)
}
return m.Cc(dst...)
} }
// Bcc sets one or more "BCC" (blind carbon copy) addresses in the mail body for the Msg. // Bcc sets one or more "BCC" (blind carbon copy) addresses in the mail body for the Msg.