Package mail
Package mail implements parsing of mail messages.
For the most part, this package follows the syntax as specified by RFC 5322.
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.
In the call graph viewer below, each node
is a function belonging to this package
and its children are the functions it
calls—perhaps dynamically.
The root nodes are the entry points of the
package: functions that may be called from
outside the package.
There may be non-exported or anonymous
functions among them if they are called
dynamically from another package.
Click a node to visit that function's source code.
From there you can visit its callers by
clicking its declaring func
token.
Functions may be omitted if they were
determined to be unreachable in the
particular programs or tests that were
analyzed.
Variables
var = errors.New("mail: header not in message")
func ParseAddressList(list string) ([]*Address, error)
ParseAddressList parses the given string as a list of addresses.
▾ Example
Code:
const list = "Alice <alice@example.com>, Bob <bob@example.com>, Eve <eve@example.com>"
emails, err := mail.ParseAddressList(list)
if err != nil {
log.Fatal(err)
}
for _, v := range emails {
fmt.Println(v.Name, v.Address)
}
Output:
Alice alice@example.com
Bob bob@example.com
Eve eve@example.com
type Address struct {
Name string
Address string
}
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(address string) (*Address, error)
Parses a single RFC 5322 address, e.g. "Barry Gibbs <bg@example.com>"
▾ Example
Code:
e, err := mail.ParseAddress("Alice <alice@example.com>")
if err != nil {
log.Fatal(err)
}
fmt.Println(e.Name, e.Address)
Output:
Alice alice@example.com
func (*Address) String
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 struct {
WordDecoder *mime.WordDecoder
}
An AddressParser is an RFC 5322 address parser.
func (*AddressParser) Parse
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
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".
type Header map[string][]string
A Header represents the key-value pairs in a mail message header.
func (h Header) AddressList(key string) ([]*Address, error)
AddressList parses the named header field as a list of addresses.
func (h Header) Date() (time.Time, error)
Date parses the Date header field.
func (h Header) Get(key string) string
Get gets the first value associated with the given key.
If there are no values associated with the key, Get returns "".
type Message struct {
Header Header
Body io.Reader
}
A Message represents a parsed mail message.
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 r.
▾ Example
Code:
msg := `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
`
r := strings.NewReader(msg)
m, err := mail.ReadMessage(r)
if err != nil {
log.Fatal(err)
}
header := m.Header
fmt.Println("Date:", header.Get("Date"))
fmt.Println("From:", header.Get("From"))
fmt.Println("To:", header.Get("To"))
fmt.Println("Subject:", header.Get("Subject"))
body, err := ioutil.ReadAll(m.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", body)
Output:
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