mirror of
https://github.com/mjl-/mox.git
synced 2025-07-12 17:04:39 +03:00
fix parsing message headers with addresses that need double quotes
we are using Go's net/mail to parse message headers. it can parse addresses, and properly decodes email addresses with double quotes (e.g. " "@example.com). however, it gives us an address without the double quotes in the localpart, effectively an invalid address. we now have a workaround to parse such not-quite-addresses. for issue #199 reported by gene-hightower, thanks for reporting!
This commit is contained in:
@ -176,6 +176,21 @@ func ParseAddress(s string) (address Address, err error) {
|
||||
return Address{lp, d}, err
|
||||
}
|
||||
|
||||
// ParseNetMailAddress parses a not-quite-valid address as found in
|
||||
// net/mail.Address.Address.
|
||||
//
|
||||
// net/mail does parse quoted addresses properly, but stores the localpart
|
||||
// unquoted. So an address `" "@example.com` would be stored as ` @example.com`,
|
||||
// which we would fail to parse without special attention.
|
||||
func ParseNetMailAddress(a string) (address Address, err error) {
|
||||
i := strings.LastIndex(a, "@")
|
||||
if i < 0 {
|
||||
return Address{}, fmt.Errorf("%w: missing @", ErrBadAddress)
|
||||
}
|
||||
addrStr := Localpart(a[:i]).String() + "@" + a[i+1:]
|
||||
return ParseAddress(addrStr)
|
||||
}
|
||||
|
||||
var ErrBadLocalpart = errors.New("invalid localpart")
|
||||
|
||||
// ParseLocalpart parses the local part.
|
||||
|
Reference in New Issue
Block a user