From c99b6c3f142863fc4c0d9c9dcd51999ebc3e4a7a Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 25 Oct 2024 19:54:41 +0200 Subject: [PATCH] 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. --- msg.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/msg.go b/msg.go index c3085c3..bd771b0 100644 --- a/msg.go +++ b/msg.go @@ -753,7 +753,16 @@ func (m *Msg) ToIgnoreInvalid(rcpts ...string) { // References: // - https://datatracker.ietf.org/doc/html/rfc5322#section-3.6.3 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.