jordan-wright / email

Robust and flexible email library for Go
MIT License
2.61k stars 324 forks source link

Extract name and email separately #114

Closed glennzw closed 4 years ago

glennzw commented 4 years ago

Is it possible to extract the sender name / email from an email object? I couldn't find an option for this in the documentation or code.

i.e from this:

e := &email.Email {
    From: "Jordan Wright <test@gmail.com>",
}

I'd like to extract "Jordan Wright" and "test@gmail.com" separately.

If not I'll just proceed with a regex, but wanted to check I wasn't missing something:

var r = regexp.MustCompile("^(.*)<(.*)>$")
e := r.FindStringSubmatch(e.From)
name := e[1]
email := e[2]
jordan-wright commented 4 years ago

I think https://golang.org/pkg/net/mail/#ParseAddress might be what you’re looking for. :)

On May 22, 2020, at 12:23 PM, Glenn Wilkinson notifications@github.com wrote:

 Is it possible to extract the sender name / email from an email object? I couldn't find an option for this in the documentation or code.

i.e from this:

e := &email.Email { From: "Jordan Wright test@gmail.com", } I'd like to extract "Jordan Wright" and "test@gmail.com" separately.

If not I'll just proceed with a regex, but wanted to check I wasn't missing something:

var r = regexp.MustCompile("^(.)<(.)>$") e := r.FindStringSubmatch(e.From) name := e[1] email := e[2] — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

glennzw commented 4 years ago

Whoops, right you are 🙈 Thanks!