From 08fe44c051c842669f9caab615bb3d4343d5490e Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 25 Oct 2024 14:57:36 +0200 Subject: [PATCH] Initialize address header map and enforce single 'From' address. This commit ensures that the address header map is properly initialized before assigning addresses to it. Additionally, it enforces the rule that only a single 'From' address is allowed, preventing multiple addresses from being set for the 'From' header. These misses were found while working on the enhanced testing suite --- msg.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/msg.go b/msg.go index 61feda1..c3085c3 100644 --- a/msg.go +++ b/msg.go @@ -583,6 +583,9 @@ func (m *Msg) SetAddrHeader(header AddrHeader, values ...string) error { // References: // - https://datatracker.ietf.org/doc/html/rfc5322#section-3.4 func (m *Msg) SetAddrHeaderIgnoreInvalid(header AddrHeader, values ...string) { + if m.addrHeader == nil { + m.addrHeader = make(map[AddrHeader][]*mail.Address) + } var addresses []*mail.Address for _, addrVal := range values { address, err := mail.ParseAddress(m.encodeString(addrVal)) @@ -591,7 +594,14 @@ func (m *Msg) SetAddrHeaderIgnoreInvalid(header AddrHeader, values ...string) { } addresses = append(addresses, address) } - m.addrHeader[header] = addresses + switch header { + case HeaderFrom: + if len(addresses) > 0 { + m.addrHeader[header] = []*mail.Address{addresses[0]} + } + default: + m.addrHeader[header] = addresses + } } // EnvelopeFrom sets the envelope from address for the Msg.