mail

package standard library
go1.26.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2026 License: BSD-3-Clause Imports: 12 Imported by: 25,782

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

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

Functions

func ParseDate added in go1.8

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

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

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

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

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

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

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

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

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

func (*AddressParser) Parse added in go1.5

func (p *