mailaddress

package module
v0.0.0-...-3743ce4 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2023 License: MIT Imports: 11 Imported by: 1

README

Build Status codecov GoDoc Go Report Card

The mailaddress package parses email addresses.

It's an alternative to net/mail; significant differences include:

  • Better errors.
  • When parsing a list it will continue to the next address on an error; this is especially useful when providing feedback to users.
  • Some useful utility functions.

Basic example:

addr, err := mailaddress.Parse(`Martin <single_address@example.com>`)

addrs, haveErr := mailaddress.ParseLint(`many@example.com, addresses@example.com`)
if haveErr {
	fmt.Println(addrs.Errors())
}

See godoc for more docs.

Documentation

Overview

Package mailaddress deals with email addresses.

It's an alternative to net/mail; significant differences include:

  • Better errors.
  • When parsing a list it will continue to the next address on an error; this is especially useful when providing feedback to users.
  • Some useful utility functions.

The Address and List types are compatible with the net/mail types.

Index

Constants

View Source
const (
	ByAddress = iota
	ByName
)

Sort keys

Variables

View Source
var (

	// ErrInvalidEncoding is used when we can't decode an address because the
	// encoding is invalid (>95% of the time this means it's spam).
	ErrInvalidEncoding = errors.New("invalid or incomplete multibyte or wide character")

	// ErrNoEmail is used when we can't find an email address at all.
	ErrNoEmail = errors.New("unable to find an email address")

	// ErrTooManyEmails is used when too many email addresses were found.
	ErrTooManyEmails = errors.New("only one address expected")

	// ErrInvalidCharacter is used when unexpected data is encountered.
	ErrInvalidCharacter = errors.New("invalid character")
)

Functions

This section is empty.

Types

type Address

type Address struct {
	Name    string `db:"name" json:"name"`
	Address string `db:"email" json:"address"`
	Raw     string `db:"-" json:"-"`
	// contains filtered or unexported fields
}

Address is a single mail address.

func New

func New(name, address string) Address

New is a shortcut to make a new Address

func Parse

func Parse(str string) (Address, error)

Parse will parse exactly one address. More than one addresses is an error, otherwise it behaves as ParseList().

func (Address) AddressEncoded

func (a Address) AddressEncoded() string

AddressEncoded returns the address ready to be put in an email header. Special characters will be appropriately escaped and RFC 2047 encoding will be applied.

func (Address) Domain

func (a Address) Domain() string

Domain gets the domain part of an address (i.e. everything after the first @).

TODO: Same as Local().

func (*Address) Error

func (a *Address) Error() error

Error returns any error that may have been associated with the mail address

func (Address) Local

func (a Address) Local() string

Local gets the local part of an address (i.e. everything before the first @).

TODO: the local part can contain a quoted/escaped @, but practically no email system deals with that, so it's not a huge deal at the moment.

func (Address) NameEncoded

func (a Address) NameEncoded() string

NameEncoded returns the name ready to be put in an email header. Special characters will be appropriately escaped and RFC 2047 encoding will be applied.

func (Address) String

func (a Address) String() string

String formats an address. It is *not* RFC 2047 encoded!

func (Address) StringEncoded

func (a Address) StringEncoded() string

StringEncoded makes a string that *is* RFC 2047 encoded

TODO: This won't work with IDN. This is okay since most email clients don't work with IDN. Last I checked this included Gmail, FastMail, Thunderbird, etc. The only client that works 100% correct AFAIK is mutt.

func (Address) ToList

func (a Address) ToList() (l List)

ToList puts this Address in an List.

func (*Address) Valid

func (a *Address) Valid() bool

Valid reports if this email looks valid. This includes some small extra checks for sanity. For example "martin@arp242 is a "valid" email address in the RFC sense, but not in the "something we can send emails to"-sense.

TODO: Perhaps consider renaming to CanSend() or Sendable() or Deliverable()?

It is also useful if the address wasn't created with ParseList() but directly (e.g. addr := Address{...}).

func (Address) WithoutTag

func (a Address) WithoutTag() string

WithoutTag gets the address with the tag part removed (if any). The tag part is everything in the local part after the first +.

type List

type List []Address

List of zero or more addresses.

func FromMap

func FromMap(m map[string]string) (l List)

FromMap creates a List from a "map[name string]email string".

func FromSlice

func FromSlice(s []string) (l List)

FromSlice creates a List from a []string. Only email addresses are set.

func NewList

func NewList(name, address string) List

NewList is a shortcut to make a new List

func ParseList

func ParseList(str string) (l List, haveError bool)

ParseList will parse one or more addresses.

func (*List) Append

func (l *List) Append(name, address string)

Append adds a new Address to the list. If the address already exists in the list this will be a noop.

func (List) ContainsAddress

func (l List) ContainsAddress(address string) bool

ContainsAddress reports if the list contains the specified email address.

func (List) ContainsDomain

func (l List) ContainsDomain(domain string) bool

ContainsDomain reports if the list contains one or more addresses with the given domain.

func (List) Errors

func (l List) Errors() (errs error)

Errors gets a list of all errors. The returned error is a multierror (github.com/hashicorp/go-multierror).

func (List) Slice

func (l List) Slice() []string

Slice gets all valid addresses in a []string slice. The names are lost and invalid addresses are skipped. Duplicates are ignored.

func (List) Sort

func (l List) Sort(key int8)

Sort the list in-place using one of the By* keys.

func (List) String

func (l List) String() string

String formats all addresses. It is *not* RFC 2047 encoded!

func (List) StringEncoded

func (l List) StringEncoded() string

StringEncoded makes a string that *is* RFC 2047 encoded. Duplicates are ignored.

func (*List) UnmarshalJSON

func (l *List) UnmarshalJSON(data []byte) error

UnmarshalJSON allows accepting several different formats for a list of mail addresses, which are (in order):

1. Standard List struct JSON string output. 2. Slice of strings containing emails. 3. Comma-separated string of emails, as accepted by ParseList().

func (List) ValidAddresses

func (l List) ValidAddresses() (valid List)

ValidAddresses returns a copy of the list which only includes valid email addresses.

Jump to

Keyboard shortcuts

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