mirror of
https://github.com/mjl-/mox.git
synced 2025-07-14 04:14:37 +03:00
recognize more charsets than utf-8/iso-8859-1/us-ascii when parsing message headers with address
as they occur in From/To headers, for example: "From: =?iso-8859-2?Q?Krist=FDna?= <k@example.com>". we are using net/mail to parse such headers. most address-parsing functions in that package will only decode charsets utf-8, iso-8859-1 and us-ascii. we have to be careful to always use net/mail.AddressParser with a WordDecoder that understands more that the basics. for issue #204 by morki, thanks for reporting!
This commit is contained in:
29
message/addr.go
Normal file
29
message/addr.go
Normal file
@ -0,0 +1,29 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/mail"
|
||||
|
||||
"github.com/mjl-/mox/smtp"
|
||||
)
|
||||
|
||||
// ParseAddressList parses a string as an address list header value
|
||||
// (potentially multiple addresses, comma-separated, with optional display
|
||||
// name).
|
||||
func ParseAddressList(s string) ([]Address, error) {
|
||||
parser := mail.AddressParser{WordDecoder: &wordDecoder}
|
||||
addrs, err := parser.ParseList(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing address list: %v", err)
|
||||
}
|
||||
r := make([]Address, len(addrs))
|
||||
for i, a := range addrs {
|
||||
addr, err := smtp.ParseNetMailAddress(a.Address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing adjusted address %q: %v", a.Address, err)
|
||||
}
|
||||
r[i] = Address{a.Name, addr.Localpart.String(), addr.Domain.ASCII}
|
||||
|
||||
}
|
||||
return r, nil
|
||||
}
|
Reference in New Issue
Block a user