9 "golang.org/x/text/unicode/norm"
11 "github.com/mjl-/mox/dns"
14// Pedantic enables stricter parsing.
17var ErrBadAddress = errors.New("invalid email address")
19// Localpart is a decoded local part of an email address, before the "@".
20// For quoted strings, values do not hold the double quote or escaping backslashes.
21// An empty string can be a valid localpart.
22// Localparts are in Unicode NFC.
25// String returns a packed representation of an address, with proper escaping/quoting, for use in SMTP.
26func (lp Localpart) String() string {
28 // First we try as dot-string. If not possible we make a quoted-string.
30 t := strings.Split(string(lp), ".")
33 if c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c > 0x7f {
37 case '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^', '_', '`', '{', '|', '}', '~':
43 dotstr = dotstr && len(e) > 0
45 dotstr = dotstr && len(t) > 0
50 // Make quoted-string.
52 for _, b := range lp {
53 if b == '"' || b == '\\' {
63// LogString returns the localpart as string for use in smtp, and an escaped
64// representation if it has non-ascii characters.
65func (lp Localpart) LogString() string {
67 qs := strconv.QuoteToASCII(s)
74// DSNString returns the localpart as string for use in a DSN.
75// utf8 indicates if the remote MTA supports utf8 messaging. If not, the 7bit DSN
76// encoding for "utf-8-addr-xtext" from RFC 6533 is used.
77func (lp Localpart) DSNString(utf8 bool) string {
83 for _, c := range lp {
84 if c > 0x20 && c < 0x7f && c != '\\' && c != '+' && c != '=' {
87 r += fmt.Sprintf(`\x{%x}`, c)
93// IsInternational returns if this is an internationalized local part, i.e. has
94// non-ASCII characters.
95func (lp Localpart) IsInternational() bool {
96 for _, c := range lp {
104// Address is a parsed email address.
107 Domain dns.Domain // todo: shouldn't we accept an ip address here too? and merge this type into smtp.Path.
110// NewAddress returns an address.
111func NewAddress(localpart Localpart, domain dns.Domain) Address {
112 return Address{localpart, domain}
115func (a Address) Path() Path {
116 return Path{Localpart: a.Localpart, IPDomain: dns.IPDomain{Domain: a.Domain}}
119func (a Address) IsZero() bool {
120 return a == Address{}
123// Pack returns the address in string form. If smtputf8 is true, the domain is
124// formatted with non-ASCII characters. If localpart has non-ASCII characters,
125// they are returned regardless of smtputf8.
126func (a Address) Pack(smtputf8 bool) string {
130 return a.Localpart.String() + "@" + a.Domain.XName(smtputf8)
133// String returns the address in string form with non-ASCII characters.
134func (a Address) String() string {
138 return a.Localpart.String() + "@" + a.Domain.Name()
141// LogString returns the address with with utf-8 in localpart and/or domain. In
142// case of an IDNA domain and/or quotable characters in the localpart, an address
143// with quoted/escaped localpart and ASCII domain is also returned.
144func (a Address) LogString() string {
149 lp := a.Localpart.String()
150 qlp := strconv.QuoteToASCII(lp)
151 escaped := qlp != `"`+lp+`"`
152 if a.Domain.Unicode != "" || escaped {
156 s += "/" + lp + "@" + a.Domain.ASCII
161// ParseAddress parses an email address. UTF-8 is allowed.
162// Returns ErrBadAddress for invalid addresses.
163func ParseAddress(s string) (address Address, err error) {
164 lp, rem, err := parseLocalPart(s)
166 return Address{}, fmt.Errorf("%w: %s", ErrBadAddress, err)
168 if !strings.HasPrefix(rem, "@") {
169 return Address{}, fmt.Errorf("%w: expected @", ErrBadAddress)
172 d, err := dns.ParseDomain(rem)
174 return Address{}, fmt.Errorf("%w: %s", ErrBadAddress, err)
176 return Address{lp, d}, err
179// ParseNetMailAddress parses a not-quite-valid address as found in
180// net/mail.Address.Address.
182// net/mail does parse quoted addresses properly, but stores the localpart
183// unquoted. So an address `" "@example.com` would be stored as ` @example.com`,
184// which we would fail to parse without special attention.
185func ParseNetMailAddress(a string) (address Address, err error) {
186 i := strings.LastIndex(a, "@")
188 return Address{}, fmt.Errorf("%w: missing @", ErrBadAddress)
190 addrStr := Localpart(a[:i]).String() + "@" + a[i+1:]
191 return ParseAddress(addrStr)
194var ErrBadLocalpart = errors.New("invalid localpart")
196// ParseLocalpart parses the local part.
198// Returns ErrBadAddress for invalid addresses.
199func ParseLocalpart(s string) (localpart Localpart, err error) {
200 lp, rem, err := parseLocalPart(s)
205 return "", fmt.Errorf("%w: remaining after localpart: %q", ErrBadLocalpart, rem)
210func parseLocalPart(s string) (localpart Localpart, remain string, err error) {
222 err = fmt.Errorf("%w: %s", ErrBadLocalpart, e)
226 return lp, p.remainder(), nil
234func (p *parser) xerrorf(format string, args ...any) {
235 panic(fmt.Errorf(format, args...))
238func (p *parser) hasPrefix(s string) bool {
239 return strings.HasPrefix(p.s[p.o:], s)
242func (p *parser) take(s string) bool {
250func (p *parser) xtake(s string) {
252 p.xerrorf("expected %q", s)
256func (p *parser) empty() bool {
257 return p.o == len(p.s)
260func (p *parser) xtaken(n int) string {
261 r := p.s[p.o : p.o+n]
266func (p *parser) remainder() string {
272// todo: reduce duplication between implementations: ../smtp/address.go:/xlocalpart ../dkim/parser.go:/xlocalpart ../smtpserver/parse.go:/xlocalpart
273func (p *parser) xlocalpart() Localpart {
276 if p.hasPrefix(`"`) {
277 s = p.xquotedString()
284 // In the wild, some services use large localparts for generated (bounce) addresses.
285 if Pedantic && len(s) > 64 || len(s) > 128 {
287 p.xerrorf("localpart longer than 64 octets")
289 return Localpart(norm.NFC.String(s))
292func (p *parser) xquotedString() string {
299 if c >= ' ' && c < 0x7f {
304 p.xerrorf("invalid localpart, bad escaped char %c", c)
313 // todo: should we be accepting utf8 for quoted strings?
314 if c >= ' ' && c < 0x7f && c != '\\' && c != '"' || c > 0x7f {
318 p.xerrorf("invalid localpart, invalid character %c", c)
322func (p *parser) xchar() rune {
323 // We are careful to track invalid utf-8 properly.
325 p.xerrorf("need another character")
329 for i, c := range p.s[p.o:] {
344func (p *parser) takefn1(what string, fn func(c rune, i int) bool) string {
346 p.xerrorf("need at least one char for %s", what)
348 for i, c := range p.s[p.o:] {
351 p.xerrorf("expected at least one char for %s, got char %c", what, c)
359func (p *parser) xatom() string {
360 return p.takefn1("atom", func(c rune, i int) bool {
362 case '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^', '_', '`', '{', '|', '}', '~':
365 return isalphadigit(c) || c > 0x7f
369func isalpha(c rune) bool {
370 return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
373func isdigit(c rune) bool {
374 return c >= '0' && c <= '9'
377func isalphadigit(c rune) bool {
378 return isalpha(c) || isdigit(c)