Fix ToFromString to handle and trim empty addresses

Previously, the ToFromString function split email addresses by commas but did not handle empty addresses or trim whitespace. Now, it trims each address and ignores any empty entries to ensure only valid addresses are processed. This prevents potential errors stemming from malformed input.
This commit is contained in:
Winni Neessen 2024-10-25 19:54:41 +02:00
parent 03da20fc39
commit c99b6c3f14
Signed by: wneessen
GPG key ID: 385AC9889632126E

11
msg.go
View file

@ -753,7 +753,16 @@ func (m *Msg) ToIgnoreInvalid(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) ToFromString(rcpts string) error { func (m *Msg) ToFromString(rcpts string) error {
return m.To(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.To(dst...)
} }
// Cc sets one or more "CC" (carbon copy) addresses in the mail body for the Msg. // Cc sets one or more "CC" (carbon copy) addresses in the mail body for the Msg.