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
179var ErrBadLocalpart = errors.New("invalid localpart")
181// ParseLocalpart parses the local part.
183// Returns ErrBadAddress for invalid addresses.
184func ParseLocalpart(s string) (localpart Localpart, err error) {
185 lp, rem, err := parseLocalPart(s)
190 return "", fmt.Errorf("%w: remaining after localpart: %q", ErrBadLocalpart, rem)
195func parseLocalPart(s string) (localpart Localpart, remain string, err error) {
207 err = fmt.Errorf("%w: %s", ErrBadLocalpart, e)
211 return lp, p.remainder(), nil
219func (p *parser) xerrorf(format string, args ...any) {
220 panic(fmt.Errorf(format, args...))
223func (p *parser) hasPrefix(s string) bool {
224 return strings.HasPrefix(p.s[p.o:], s)
227func (p *parser) take(s string) bool {
235func (p *parser) xtake(s string) {
237 p.xerrorf("expected %q", s)
241func (p *parser) empty() bool {
242 return p.o == len(p.s)
245func (p *parser) xtaken(n int) string {
246 r := p.s[p.o : p.o+n]
251func (p *parser) remainder() string {
257// todo: reduce duplication between implementations: ../smtp/address.go:/xlocalpart ../dkim/parser.go:/xlocalpart ../smtpserver/parse.go:/xlocalpart
258func (p *parser) xlocalpart() Localpart {
261 if p.hasPrefix(`"`) {
262 s = p.xquotedString()
269 // In the wild, some services use large localparts for generated (bounce) addresses.
270 if Pedantic && len(s) > 64 || len(s) > 128 {
272 p.xerrorf("localpart longer than 64 octets")
274 return Localpart(norm.NFC.String(s))
277func (p *parser) xquotedString() string {
284 if c >= ' ' && c < 0x7f {
289 p.xerrorf("invalid localpart, bad escaped char %c", c)
298 // todo: should we be accepting utf8 for quoted strings?
299 if c >= ' ' && c < 0x7f && c != '\\' && c != '"' || c > 0x7f {
303 p.xerrorf("invalid localpart, invalid character %c", c)
307func (p *parser) xchar() rune {
308 // We are careful to track invalid utf-8 properly.
310 p.xerrorf("need another character")
314 for i, c := range p.s[p.o:] {
329func (p *parser) takefn1(what string, fn func(c rune, i int) bool) string {
331 p.xerrorf("need at least one char for %s", what)
333 for i, c := range p.s[p.o:] {
336 p.xerrorf("expected at least one char for %s, got char %c", what, c)
344func (p *parser) xatom() string {
345 return p.takefn1("atom", func(c rune, i int) bool {
347 case '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^', '_', '`', '{', '|', '}', '~':
350 return isalphadigit(c) || c > 0x7f
354func isalpha(c rune) bool {
355 return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
358func isdigit(c rune) bool {
359 return c >= '0' && c <= '9'
362func isalphadigit(c rune) bool {
363 return isalpha(c) || isdigit(c)