version: 1.10

package mail

import "net/mail"

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:

  1. * Obsolete address formats are not parsed, including addresses with
  2. embedded route information.
  3. * The full range of spacing (the CFWS syntax element) is not supported,
  4. such as breaking addresses across lines.
  5. * No unicode normalization is performed.
  6. * The special characters ()[]:;@\, are allowed to appear unquoted in names.

Index

Examples

Package files

message.go

Variables

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

func ParseAddressList

  1. func ParseAddressList(list string) ([]*Address, 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. fmt.Println(v.Name, v.Address)
  8. }
  9. // Output:
  10. // Alice alice@example.com
  11. // Bob bob@example.com
  12. // Eve eve@example.com

func ParseDate

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

ParseDate parses an RFC 5322 date string.

type Address

  1. type Address struct {
  2. Name string // Proper name; may be empty.
  3. Address string // 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:
bg@example.com”}.

func ParseAddress

  1. func ParseAddress(address string) (*Address, error)

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


Example:

  1. e, err := mail.ParseAddress("Alice <alice@example.com>")
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. fmt.Println(e.Name, e.Address)
  6. // Output:
  7. // Alice alice@example.com

func (*Address) String

  1. func (a *Address) String() 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.WordDecoder
  4. }

An AddressParser is an RFC 5322 address parser.

func (*AddressParser) Parse

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

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

func (*AddressParser) ParseList

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

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

  1. type Header map[string][]string

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

func (Header) AddressList

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

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

func (Header) Date

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

Date parses the Date header field.

func (Header) Get

  1. func (h Header) Get(key string) 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.

type Message

  1. type Message struct {
  2. Header Header
  3. Body io.Reader
  4. }

A Message represents a parsed mail message.

func ReadMessage

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

ReadMessage reads a message from r. The headers are parsed, and the body of the
message will be available for reading from msg.Body.


Example:

  1. msg := `Date: Mon, 23 Jun 2015 11:40:36 -0400
  2. From: Gopher <from@example.com>
  3. To: Another Gopher <to@example.com>
  4. Subject: Gophers at Gophercon
  5. Message body
  6. `
  7. r := strings.NewReader(msg)
  8. m, err := mail.ReadMessage(r)
  9. if err != nil {
  10. log.Fatal(err)
  11. }
  12. header := m.Header
  13. fmt.Println("Date:", header.Get("Date"))
  14. fmt.Println("From:", header.Get("From"))
  15. fmt.Println("To:", header.Get("To"))
  16. fmt.Println("Subject:", header.Get("Subject"))
  17. body, err := ioutil.ReadAll(m.Body)
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. fmt.Printf("%s", body)
  22. // Output:
  23. // Date: Mon, 23 Jun 2015 11:40:36 -0400
  24. // From: Gopher <from@example.com>
  25. // To: Another Gopher <to@example.com>
  26. // Subject: Gophers at Gophercon
  27. // Message body