1package message
2
3import (
4 "fmt"
5 "net/mail"
6
7 "github.com/mjl-/mox/smtp"
8)
9
10// ParseAddressList parses a string as an address list header value
11// (potentially multiple addresses, comma-separated, with optional display
12// name).
13func ParseAddressList(s string) ([]Address, error) {
14 parser := mail.AddressParser{WordDecoder: &wordDecoder}
15 addrs, err := parser.ParseList(s)
16 if err != nil {
17 return nil, fmt.Errorf("parsing address list: %v", err)
18 }
19 r := make([]Address, len(addrs))
20 for i, a := range addrs {
21 addr, err := smtp.ParseNetMailAddress(a.Address)
22 if err != nil {
23 return nil, fmt.Errorf("parsing adjusted address %q: %v", a.Address, err)
24 }
25 r[i] = Address{a.Name, addr.Localpart.String(), addr.Domain.ASCII}
26
27 }
28 return r, nil
29}
30