emailaddress

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2023 License: MIT Imports: 6 Imported by: 31

README

go-emailaddress

GoDoc Build Status Test Coverage go report

go-emailaddress is a tiny Go library for finding, parsing and validating email addresses. This library is tested for Go v1.9 and above.

Note that there is no such thing as perfect email address validation other than sending an actual email (ie. with a confirmation token). This library however checks if the email format conforms to the spec and if the host (domain) is actually able to receive emails. You can also use this library to find emails in a byte array. This package was created as similar packages don't seem to be maintained anymore (ie contain bugs with pull requests still open), and/or use wrong local validation.

Usage

go get -u github.com/mcnijman/go-emailaddress
Parsing and local validation

Parse and validate the email locally using RFC 5322 regex, note that when err == nil it doesn't necessarily mean the email address actually exists.

import "github.com/mcnijman/go-emailaddress"

email, err := emailaddress.Parse("foo@bar.com")
if err != nil {
    fmt.Println("invalid email")
}

fmt.Println(email.LocalPart) // foo
fmt.Println(email.Domain) // bar.com
fmt.Println(email) // foo@bar.com
fmt.Println(email.String()) // foo@bar.com
Validating the host

Host validation will first attempt to resolve the domain and then verify if we can start a mail transaction with the host. This is relatively slow as it will contact the host several times. Note that when err == nil it doesn't necessarily mean the email address actually exists.

import "github.com/mcnijman/go-emailaddress"

email, err := emailaddress.Parse("foo@bar.com")
if err != nil {
    fmt.Println("invalid email")
}

err := email.ValidateHost()
if err != nil {
    fmt.Println("invalid host")
}
Validating that the publix suffix is ICANN managed

Whether the public suffix is managed by the Internet Corporation for Assigned Names and Numbers. If not an error is returned and the public suffix is privately managed. For example, foo.org and foo.co.uk are ICANN domains, foo.dyndns.org and foo.blogspot.co.uk are private domains. More information on publix suffixes here.

import "github.com/mcnijman/go-emailaddress"

email, err := emailaddress.Parse("foo@bar.com")
if err != nil {
    fmt.Println("invalid email")
}

err := email.ValidateIcanSuffix()
if err != nil {
    fmt.Println("not an icann suffix")
}
Finding emails

This will look for emails in a byte array (ie text or an html response).

import "github.com/mcnijman/go-emailaddress"

text := []byte(`Send me an email at foo@bar.com or foo@domain.fakesuffix.`)
validateHost := false

emails := emailaddress.Find(text, validateHost)

for _, e := range emails {
    fmt.Println(e)
}
// foo@bar.com
// foo@domain.fakesuffix

As RFC 5322 is really broad this method will likely match images and urls that contain the '@' character (ie. !--logo@2x.png). For more reliable results, you can use the following method.

import "github.com/mcnijman/go-emailaddress"

text := []byte(`Send me an email at foo@domain.com or foo@domain.fakesuffix.`)
validateHost := false

emails := emailaddress.FindWithIcannSuffix(text, validateHost)

for _, e := range emails {
    fmt.Println(e)
}
// foo@bar.com

Versioning

This library uses semantic versioning 2.0.0.

License

This library is distributed under the MIT license found in the LICENSE file.

Documentation

Overview

Package emailaddress provides a tiny library for finding, parsing and validation of email addresses. This library is tested for Go v1.9 and above.

go get -u github.com/mcnijman/go-emailaddress

Local validation

Parse and validate the email locally using RFC 5322 regex, note that when err == nil it doesn't necessarily mean the email address actually exists.

import "github.com/mcnijman/go-emailaddress"

email, err := emailaddress.Parse("foo@bar.com")
if err != nil {
	fmt.Println("invalid email")
}

fmt.Println(email.LocalPart) // foo
fmt.Println(email.Domain) // bar.com
fmt.Println(email) // foo@bar.com
fmt.Println(email.String()) // foo@bar.com

Host validation

Host validation will first attempt to resolve the domain and then verify if we can start a mail transaction with the host. This is relatively slow as it will contact the host several times. Note that when err == nil it doesn't necessarily mean the email address actually exists.

import "github.com/mcnijman/go-emailaddress"

email, err := emailaddress.Parse("foo@bar.com")
if err != nil {
	fmt.Println("invalid email")
}

err := email.ValidateHost()
if err != nil {
	fmt.Println("invalid host")
}

Finding emails

This will look for emails in a byte array (ie text or an html response).

import "github.com/mcnijman/go-emailaddress"

text := []byte(`Send me an email at foo@bar.com.`)
validateHost := false

emails := emailaddress.Find(text, validateHost)

for _, e := range emails {
	fmt.Println(e)
}
// foo@bar.com

As RFC 5322 is really broad this method will likely match images and urls that contain the '@' character (ie. !--logo@2x.png). For more reliable results, you can use the following method.

import "github.com/mcnijman/go-emailaddress"

text := []byte(`Send me an email at foo@bar.com or fake@domain.foobar.`)
validateHost := false

emails := emailaddress.FindWithIcannSuffix(text, validateHost)

for _, e := range emails {
	fmt.Println(e)
}
// foo@bar.com

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LookupHost added in v1.1.1

func LookupHost(domain string) (string, error)

LookupHost first checks if any MX records are available and if not, it will check if A records are available as they can resolve email server hosts. An error indicates that non of the A or MX records are available.

func TryHost added in v1.1.1

func TryHost(host string, e EmailAddress) error

TryHost will verify if we can start a mail transaction with the host. A lot of hosts block this method so don't expect much from it.

Types

type EmailAddress

type EmailAddress struct {
	// LocalPart usually the username of an email address.
	LocalPart string

	// Domain is the part of the email address after the last @.
	// This should be DNS resolvable to an email server.
	Domain string
}

EmailAddress is a structure that stores the address local-part@domain parts.

func Find

func Find(haystack []byte, validateHost bool) (emails []*EmailAddress)

Find uses the a stricter regex than the RFC 5322 and matches emails that are more likely to be real. Since the RFC 5322 spec is looser, it can miss emails that are real, but will more likely have better results. See examples in the tests.

func FindWithIcannSuffix

func FindWithIcannSuffix(haystack []byte, validateHost bool) (emails []*EmailAddress)

FindWithIcannSuffix uses the RFC 5322 regex to match, parse and validate any email addresses found in a string. It will return emails if its eTLD is managed by the ICANN organization. If the validateHost boolean is true it will call the validate host for every email address encountered. As RFC 5322 is really broad this method will likely match images and urls that contain the '@' character.

func FindWithRFC5322 added in v1.1.1

func FindWithRFC5322(haystack []byte, validateHost bool) (emails []*EmailAddress)

FindWithRFC5322 uses the RFC 5322 regex to match, parse and validate any email addresses found in a string. If the validateHost boolean is true it will call the validate host for every email address encountered. As RFC 5322 is really broad this method will likely match images and urls that contain the '@' character.

func Parse

func Parse(email string) (*EmailAddress, error)

Parse will parse the input and validate the email locally. If you want to validate the host of this email address remotely call the ValidateHost method.

func (EmailAddress) String

func (e EmailAddress) String() string

func (EmailAddress) ValidateHost

func (e EmailAddress) ValidateHost() error

ValidateHost will test if the email address is actually reachable. It will first try to resolve the host and then start a mail transaction.

func (EmailAddress) ValidateIcanSuffix

func (e EmailAddress) ValidateIcanSuffix() error

ValidateIcanSuffix will test if the public suffix of the domain is managed by ICANN using the golang.org/x/net/publicsuffix package. If not it will return an error. Note that if this method returns an error it does not necessarily mean that the email address is invalid. Also the suffix list in the standard package is embedded and thereby not up to date.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL