Go

Go

By DeepSource

Use (*mail.Address).String() instead of fmt.Sprintf for mail address GO-W1031

Bug risk Autofix

For a name and address that is supposed to formatted to a single mail address, it is recommended to use net/mail's (*Address).String instead of manually formatting using fmt.Sprintf which makes the code much more reliable.

(*mail.Address).String() formats the name and address to a mail address as follows:

Formats the address as a valid RFC 5322 address. If the address's name contains non-ASCII characters the name will be rendered according to RFC 2047.

Bad practice

addr = fmt.Sprintf(`"%s" <%s>`, name, email)
addr = fmt.Sprintf(`"%s"<%s>`, name, email)
addr = fmt.Sprintf(""%s"<%s>", name, email)
addr = fmt.Sprintf(""%s" <%s>", name, email)
addr = fmt.Sprintf("%s<%s>", name, email)
addr = fmt.Sprintf(`"%s" <%s>`, "name", "email")

Recommended

addr = (&mail.Address{Name: name, Address: email}).String()
addr = (&mail.Address{Name: name, Address: email}).String()
addr = (&mail.Address{Name: name, Address: email}).String()
addr = (&mail.Address{Name: name, Address: email}).String()
addr = (&mail.Address{Name: name, Address: email}).String()
addr = (&mail.Address{Name: "name", Address: "email"}).String()