Run Format

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:

* Obsolete address formats are not parsed, including addresses with
  embedded route information.
* Group addresses are not parsed.
* The full range of spacing (the CFWS syntax element) is not supported,
  such as breaking addresses across lines.
* No unicode normalization is performed.


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

func ParseAddressList

ParseAddressList parses the given string as a list of addresses.

func ParseAddressList(list string) ([]*Address, error)


Alice alice@example.com
Bob bob@example.com
Eve eve@example.com

type Address

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"}.

type Address struct {
        Name    string // Proper name; may be empty.
        Address string // user@domain

func ParseAddress

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

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


Alice alice@example.com

func (*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.

func (a *Address) String() string

type AddressParser

An AddressParser is an RFC 5322 address parser.

type AddressParser struct {
        // WordDecoder optionally specifies a decoder for RFC 2047 encoded-words.
        WordDecoder *mime.WordDecoder

func (*AddressParser) Parse

Parse parses a single RFC 5322 address of the form "Gogh Fir <gf@example.com>" or "foo@example.com".

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

func (*AddressParser) ParseList

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

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

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

type Header map[string][]string

func (Header) AddressList

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

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

func (Header) Date

Date parses the Date header field.

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

func (Header) Get

Get gets the first value associated with the given key. If there are no values associated with the key, Get returns "".

func (h Header) Get(key string) string

type Message

A Message represents a parsed mail message.

type Message struct {
        Header Header
        Body   io.Reader

func ReadMessage

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

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


Date: Mon, 23 Jun 2015 11:40:36 -0400
From: Gopher <from@example.com>
To: Another Gopher <to@example.com>
Subject: Gophers at Gophercon
Message body