package mail
import "net/mail"
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 ¶
- Variables
- func ParseDate(date string) (time.Time, error)
- type Address
- func ParseAddress(address string) (*Address, error)
- func ParseAddressList(list string) ([]*Address, error)
- func (a *Address) String() string
- type AddressParser
- func (p *AddressParser) Parse(address string) (*Address, error)
- func (p *AddressParser) ParseList(list string) ([]*Address, error)
- type Header
- func (h Header) AddressList(key string) ([]*Address, error)
- func (h Header) Date() (time.Time, error)
- func (h Header) Get(key string) string
- type Message
Examples ¶
Variables ¶
Functions ¶
func ParseDate ¶
ParseDate parses an RFC 5322 date string.
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 ¶
ParseAddress parses a single RFC 5322 address, e.g. "Barry Gibbs <bg@example.com>"
func ParseAddressList ¶
ParseAddressList parses the given string as a list of addresses.
Code:play
Output: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)
}
}
Alice alice@example.com
Bob bob@example.com
Eve eve@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.
type AddressParser ¶
type AddressParser struct { // WordDecoder optionally specifies a decoder for RFC 2047 encoded-words. 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 ¶
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) Date ¶
Date parses the Date header field.
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.
Code:play
Output: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)
}
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
Source Files ¶
- Version
- v1.24.0 (latest)
- Published
- Feb 10, 2025
- Platform
- linux/amd64
- Imports
- 12 packages
- Last checked
- 52 seconds ago –
Tools for package owners.