diff options
| author | James Elliott <james-d-elliott@users.noreply.github.com> | 2021-11-30 22:15:21 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-30 22:15:21 +1100 |
| commit | ab8f9b0697efe4987c76a3ca2d4758b35ac8f2ad (patch) | |
| tree | 2eebbb5b4fc46ab3302c117d355d8d333350e450 /internal/configuration/decode_hooks.go | |
| parent | 568f210b2b764ebc0a18295bdf0589b66e9291d9 (diff) | |
fix(notifier): force use of sender email in smtp from cmd (#2616)
This change addresses an issue with the usage of the full sender configuration option in the MAIL FROM SMTP command. If a user includes a name in the sender this shouldn't be sent in the MAIL FROM command, instead we should extract it and use just the email portion.
Fixes #2571
Diffstat (limited to 'internal/configuration/decode_hooks.go')
| -rw-r--r-- | internal/configuration/decode_hooks.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/internal/configuration/decode_hooks.go b/internal/configuration/decode_hooks.go new file mode 100644 index 000000000..b2012d9ff --- /dev/null +++ b/internal/configuration/decode_hooks.go @@ -0,0 +1,35 @@ +package configuration + +import ( + "fmt" + "net/mail" + "reflect" + + "github.com/mitchellh/mapstructure" +) + +// StringToMailAddressFunc decodes a string into a mail.Address. +func StringToMailAddressFunc() mapstructure.DecodeHookFunc { + return func(f reflect.Kind, t reflect.Kind, data interface{}) (value interface{}, err error) { + if f != reflect.String || t != reflect.TypeOf(mail.Address{}).Kind() { + return data, nil + } + + dataStr := data.(string) + + if dataStr == "" { + return mail.Address{}, nil + } + + var ( + mailAddress *mail.Address + ) + + mailAddress, err = mail.ParseAddress(dataStr) + if err != nil { + return nil, fmt.Errorf("could not parse '%s' as a RFC5322 address: %w", dataStr, err) + } + + return *mailAddress, nil + } +} |
