Documentation
ΒΆ
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.
- The full range of spacing (the CFWS syntax element) is not supported, such as breaking addresses across lines.
- No unicode normalization is performed.
- A leading From line is permitted, as in mbox format (RFC 4155).
Index ΒΆ
Examples ΒΆ
Constants ΒΆ
This section is empty.
Variables ΒΆ
var ErrHeaderNotPresent = errors.New("mail: header not in message")
Functions ΒΆ
func ParseDate ΒΆ added in go1.8
ParseDate parses an RFC 5322 date string.
Example ΒΆ
package main
import (
"fmt"
"log"
"net/mail"
"time"
)
func main() {
dateStr := "Wed, 09 Oct 2024 09:55:06 -0700"
t, err := mail.ParseDate(dateStr)
if err != nil {
log.Fatalf("Failed to parse date: %v", err)
}
fmt.Println(t.Format(time.RFC3339))
}
Output: 2024-10-09T09:55:06-07:00
Types ΒΆ
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"}.
func ParseAddress ΒΆ added in go1.1
ParseAddress parses a single RFC 5322 address, e.g. "Barry Gibbs <bg@example.com>"
Example ΒΆ
package main
import (
"fmt"
"log"
"net/mail"
)
func main() {
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 ParseAddressList ΒΆ added in go1.1
ParseAddressList parses the given string as a list of addresses.
Example ΒΆ
package main
import (
"fmt"
"log"
"net/mail"
)
func main() {
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 AddressParser ΒΆ added in go1.5
type AddressParser struct {
// WordDecoder optionally specifies a decoder for RFC 2047 encoded-words.
WordDecoder *mime.WordDecoder
}
An AddressParser is an RFC 5322 address parser.
type Header ΒΆ
A Header represents the key-value pairs in a mail message header.
func (Header) AddressList ΒΆ
AddressList parses the named header field as a list of addresses.
func (Header) Get ΒΆ
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 ΒΆ
A Message represents a parsed mail message.
func ReadMessage ΒΆ
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 ΒΆ
package main
import (
"fmt"
"io"
"log"
"net/mail"
"strings"
)
func main() {
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 := io.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