go-gomail / gomail

The best way to send emails in Go.
MIT License
4.3k stars 570 forks source link

Make encodeHeader return a new array #144

Closed Emile-Filteau closed 4 years ago

Emile-Filteau commented 4 years ago

Problem

Arrays in Go are mutable. This method currently replaces all values passed to SetHeader by their encoded version.

This can be problematic in a scenario that the array passed to SetHeader is reused somewhere else in the code.

Ex :

rcptTo := []string{"alice@example.com, "bob@example.com"}

message := gomail.NewMessage()
message.SetHeader("To", rcptTo)
// ...
var buffer bytes.Buffer
message.WriteTo(&buffer)

mail.SendMail("myhost.com:25", nil, "postmaster@example.com", rcptTo, buffer.Bytes())

In the above example, the recipients passed to SendMail will be encoded and result in a 501 5.1.3 Bad recipient address syntax from any postfix server.

In my opinion, the expected behaviour of SetHeader is not to mutate the values passed as arguments.

Solution

The proposed change creates a new array of string of the same length as the header values passed in and fill it with the encoded values, therefore not touching the raw ones.

pedromorgan commented 4 years ago

see #104 and #108

Emile-Filteau commented 4 years ago

Thanks, will open on the active fork