Skip to content

net/mail: Address and ParseAddress are not symmetric with parentheses #13966

Closed
@chowey

Description

@chowey

In the net/mail package, Address is used to generate a valid RFC 5322 address. ParseAddress is used to parse a single RFC 5322 address.

There is currently a bug if the address name contains parentheses.

The following code fails to run:

package main

import "fmt"
import "net/mail"

func main() {

    tests := []*mail.Address{
        {Name: "Robert Müller", Address: "bob@example.com"},
        {Name: "Robert (Bob) Müller", Address: "bob@example.com"},
    }

    for i, in := range tests {
        fmt.Printf("\nTest %d\n", i)

        // straight encoding, will use q-encoding if needed
        fmt.Printf("In:  %s\n", in)

        // decode the q-encoding
        out, err := mail.ParseAddress(in.String())
        if err != nil {
            // oops! cannot parse our own address?
            panic(err)
        }

        // should match our earlier q-encoding
        fmt.Printf("Out: %s\n", out)
    }
}

Expected output:

Test 0
In:  =?utf-8?q?Robert_M=C3=BCller?= <bob@example.com>
Out: =?utf-8?q?Robert_M=C3=BCller?= <bob@example.com>

Test 1
In:  =?utf-8?q?Robert_(Bob)_M=C3=BCller?= <bob@example.com>
Out: =?utf-8?q?Robert_(Bob)_M=C3=BCller?= <bob@example.com>

Actual output:

Test 0
In:  =?utf-8?q?Robert_M=C3=BCller?= <bob@example.com>
Out: =?utf-8?q?Robert_M=C3=BCller?= <bob@example.com>

Test 1
In:  =?utf-8?q?Robert_(Bob)_M=C3=BCller?= <bob@example.com>
panic: mail: no angle-addr

goroutine 1 [running]:
main.main()

See it in action here.

These functions should be symmetric if I correctly understand the documentation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions