Enhance address header methods with detailed documentation

Updated comments in `header.go` and `msg.go` to provide more detailed explanations and references to RFC 5322. This improves clarity on how headers are set and utilized, and the conditions under which they operate.
This commit is contained in:
Winni Neessen 2024-10-05 13:56:47 +02:00
parent 78e2857782
commit 1dcdad9da1
Signed by: wneessen
GPG key ID: 385AC9889632126E
2 changed files with 26 additions and 7 deletions

View file

@ -105,8 +105,11 @@ const (
// HeaderCc is the "Carbon Copy" header field. // HeaderCc is the "Carbon Copy" header field.
HeaderCc AddrHeader = "Cc" HeaderCc AddrHeader = "Cc"
// HeaderEnvelopeFrom is the envelope FROM header field. It is not included in the mail body but only used by // HeaderEnvelopeFrom is the envelope FROM header field.
// the Client for the envelope. //
// It is generally not included in the mail body but only used by the Client for the communication with the
// SMTP server. If the Msg has no "FROM" address set in the mail body, the msgWriter will try to use the
// envelope from address, if this has been set for the Msg.
HeaderEnvelopeFrom AddrHeader = "EnvelopeFrom" HeaderEnvelopeFrom AddrHeader = "EnvelopeFrom"
// HeaderFrom is the "From" header field. // HeaderFrom is the "From" header field.

26
msg.go
View file

@ -338,7 +338,13 @@ func (m *Msg) SetGenHeaderPreformatted(header Header, value string) {
m.preformHeader[header] = value m.preformHeader[header] = value
} }
// SetAddrHeader sets an address related header field of the Msg // SetAddrHeader sets the specified AddrHeader for the Msg to the given values.
//
// Addresses are parsed according to RFC 5322. If parsing of ANY of the provided values fails,
// and error is returned. If you cannot guarantee that all provided values are valid, you can
// use SetAddrHeaderIgnoreInvalid instead, which will skip any parsing error silently.
//
// https://datatracker.ietf.org/doc/html/rfc5322#section-3.4
func (m *Msg) SetAddrHeader(header AddrHeader, values ...string) error { func (m *Msg) SetAddrHeader(header AddrHeader, values ...string) error {
if m.addrHeader == nil { if m.addrHeader == nil {
m.addrHeader = make(map[AddrHeader][]*mail.Address) m.addrHeader = make(map[AddrHeader][]*mail.Address)
@ -362,8 +368,12 @@ func (m *Msg) SetAddrHeader(header AddrHeader, values ...string) error {
return nil return nil
} }
// SetAddrHeaderIgnoreInvalid sets an address related header field of the Msg and ignores invalid address // SetAddrHeaderIgnoreInvalid sets the specified AddrHeader for the Msg to the given values.
// in the validation process //
// Addresses are parsed according to RFC 5322. If parsing of ANY of the provided values fails,
// the error is ignored and the address omiitted from the address list.
//
// https://datatracker.ietf.org/doc/html/rfc5322#section-3.4
func (m *Msg) SetAddrHeaderIgnoreInvalid(header AddrHeader, values ...string) { func (m *Msg) SetAddrHeaderIgnoreInvalid(header AddrHeader, values ...string) {
var addresses []*mail.Address var addresses []*mail.Address
for _, addrVal := range values { for _, addrVal := range values {
@ -376,8 +386,14 @@ func (m *Msg) SetAddrHeaderIgnoreInvalid(header AddrHeader, values ...string) {
m.addrHeader[header] = addresses m.addrHeader[header] = addresses
} }
// EnvelopeFrom takes and validates a given mail address and sets it as envelope "FROM" // EnvelopeFrom sets the envelope from address for the Msg.
// addrHeader of the Msg //
// The HeaderEnvelopeFrom address is generally not included in the mail body but only used by the Client for the
// communication with the SMTP server. If the Msg has no "FROM" address set in the mail body, the msgWriter will
// try to use the envelope from address, if this has been set for the Msg. The provided address is validated
// according to RFC 5322 and will return an error if the validation fails.
//
// https://datatracker.ietf.org/doc/html/rfc5322#section-3.4
func (m *Msg) EnvelopeFrom(from string) error { func (m *Msg) EnvelopeFrom(from string) error {
return m.SetAddrHeader(HeaderEnvelopeFrom, from) return m.SetAddrHeader(HeaderEnvelopeFrom, from)
} }