Overview

Package mail implements parsing of mail messages.

For the most part, this package follows the syntax as specified by RFC 5322 and
extended by RFC 6532. Notable divergences:

Index

Package files

  1. var ErrHeaderNotPresent = errors.("mail: header not in message")

func ParseAddressList

  1. func ParseAddressList(list string) ([]*, error)

ParseAddressList parses the given string as a list of addresses.


Example:

  1. const list = "Alice <alice@example.com>, Bob <bob@example.com>, Eve <eve@example.com>"
  2. emails, err := mail.ParseAddressList(list)
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. for _, v := range emails {
  7. }
  8. // Output:
  9. // Alice alice@example.com
  10. // Bob bob@example.com
  11. // Eve eve@example.com

func ParseDate

  1. func ParseDate(date string) (.Time, )

ParseDate parses an RFC 5322 date string.

  1. type Address struct {
  2. Name string // Proper name; may be empty.
  3. Address // user@domain
  4. }

Address represents a single mail address. An address such as “Barry Gibbs
bg@example.com“ is represented as Address{Name: “Barry Gibbs”, Address:
“”}.

func ParseAddress

Parses a single RFC 5322 address, e.g. “Barry Gibbs bg@example.com


Example:

  1. log.Fatal(err)
  2. }
  3. fmt.Println(e.Name, e.Address)
  4. // Output:
  5. // Alice alice@example.com

  1. func (a *Address) String()

String formats the address as a valid RFC 5322 address. If the address’s name
contains non-ASCII characters the name will be rendered according to RFC 2047.

type AddressParser

  1. type AddressParser struct {
  2. // WordDecoder optionally specifies a decoder for RFC 2047 encoded-words.
  3. WordDecoder *mime.
  4. }

An AddressParser is an RFC 5322 address parser.

func (*AddressParser) Parse

  1. func (p *AddressParser) Parse(address ) (*Address, )

Parse parses a single RFC 5322 address of the form “Gogh Fir gf@example.com
or “”.

func (*AddressParser) ParseList

  1. func (p *AddressParser) ParseList(list ) ([]*Address, )

ParseList parses the given string as a list of comma-separated addresses of the
form “Gogh Fir gf@example.com“ or “”.

A Header represents the key-value pairs in a mail message header.

  1. func (h Header) AddressList(key ) ([]*Address, )

AddressList parses the named header field as a list of addresses.

func (Header) Date

  1. func (h Header) Date() (.Time, )

Date parses the Date header field.

func (Header) Get

  1. func (h Header) Get(key ) string

Get gets the first value associated with the given key. It is case insensitive;
CanonicalMIMEHeaderKey is used to canonicalize the provided key. If there are no
values associated with the key, Get returns “”. To access multiple values of a
key, or to use non-canonical keys, access the map directly.

  1. type Message struct {
  2. Header
  3. Body io.

A Message represents a parsed mail message.

  1. func ReadMessage(r io.) (msg *Message, err )


Example: