-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
recognize more charsets than utf-8/iso-8859-1/us-ascii when parsing m…
…essage headers with address as they occur in From/To headers, for example: "From: =?iso-8859-2?Q?Krist=FDna?= <[email protected]>". 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!
- Loading branch information
Showing
6 changed files
with
64 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package message | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestParseAddressList(t *testing.T) { | ||
l, err := ParseAddressList("=?iso-8859-2?Q?Krist=FDna?= <[email protected]>, [email protected]") | ||
tcheck(t, err, "parsing address list") | ||
tcompare(t, l, []Address{{"Kristýna", "k", "example.com"}, {"", "mjl", "mox.example"}}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -603,3 +603,10 @@ func TestNetMailAddress(t *testing.T) { | |
tcheck(t, err, "parse") | ||
tcompare(t, p.Envelope.From, []Address{{"", `" "`, "example.com"}}) | ||
} | ||
|
||
func TestParseQuotedCharset(t *testing.T) { | ||
const s = "From: =?iso-8859-2?Q?Krist=FDna?= <[email protected]>\r\n\r\nbody\r\n" | ||
p, err := EnsurePart(pkglog.Logger, false, strings.NewReader(s), int64(len(s))) | ||
tcheck(t, err, "parse") | ||
tcompare(t, p.Envelope.From, []Address{{"Kristýna", "k", "example.com"}}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters