Documentation ¶
Overview ¶
Package mail implements reading and writing mail messages.
This package assumes that a mail message contains one or more text parts and zero or more attachment parts. Each text part represents a different version of the message content (e.g. a different type, a different language and so on).
RFC 5322 defines the Internet Message Format.
Index ¶
- func CreateSingleInlineWriter(w io.Writer, header Header) (io.WriteCloser, error)
- type Address
- type AttachmentHeader
- type Header
- func (h *Header) AddressList(key string) ([]*Address, error)
- func (h *Header) Copy() Header
- func (h *Header) Date() (time.Time, error)
- func (h *Header) GenerateMessageID() error
- func (h *Header) GenerateMessageIDWithHostname(hostname string) error
- func (h *Header) MessageID() (string, error)
- func (h *Header) MsgIDList(key string) ([]string, error)
- func (h *Header) SetAddressList(key string, addrs []*Address)
- func (h *Header) SetDate(t time.Time)
- func (h *Header) SetMessageID(id string)
- func (h *Header) SetMsgIDList(key string, l []string)
- func (h *Header) SetSubject(s string)
- func (h *Header) Subject() (string, error)
- type InlineHeader
- type InlineWriter
- type Part
- type PartHeader
- type Reader
- type Writer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateSingleInlineWriter ¶ added in v0.10.0
CreateSingleInlineWriter writes a mail header to w. The mail will contain a single inline part. The body of the part should be written to the returned io.WriteCloser. Only one single inline part should be written, use CreateWriter if you want multiple parts.
Types ¶
type Address ¶
Address represents a single mail address. The type alias ensures that a net/mail.Address can be used wherever an Address is expected
func ParseAddress ¶ added in v0.14.0
ParseAddress parses a single RFC 5322 address, e.g. "Barry Gibbs <bg@example.com>" Use this function only if you parse from a string, if you have a Header use Header.AddressList instead
func ParseAddressList ¶ added in v0.14.0
ParseAddressList parses the given string as a list of addresses. Use this function only if you parse from a string, if you have a Header use Header.AddressList instead
type AttachmentHeader ¶
type AttachmentHeader struct {
message.Header
}
An AttachmentHeader represents an attachment's header.
func (*AttachmentHeader) Filename ¶
func (h *AttachmentHeader) Filename() (string, error)
Filename parses the attachment's filename.
func (*AttachmentHeader) SetFilename ¶
func (h *AttachmentHeader) SetFilename(filename string)
SetFilename formats the attachment's filename.
type Header ¶
type Header struct {
message.Header
}
A Header is a mail header.
func HeaderFromMap ¶ added in v0.15.0
HeaderFromMap creates a header from a map of header fields.
This function is provided for interoperability with the standard library. If possible, ReadHeader should be used instead to avoid loosing information. The map representation looses the ordering of the fields, the capitalization of the header keys, and the whitespace of the original header.
func (*Header) AddressList ¶
AddressList parses the named header field as a list of addresses. If the header field is missing, it returns nil.
This can be used on From, Sender, Reply-To, To, Cc and Bcc header fields.
func (*Header) Date ¶
Date parses the Date header field. If the header field is missing, it returns the zero time.
func (*Header) GenerateMessageID ¶ added in v0.13.0
GenerateMessageID wraps GenerateMessageIDWithHostname and therefore uses the hostname of the local machine. This is done to not break existing software. Wherever possible better use GenerateMessageIDWithHostname, because the local hostname of a machine tends to not be unique nor a FQDN which especially brings problems with spam filters.
func (*Header) GenerateMessageIDWithHostname ¶ added in v0.17.0
GenerateMessageIDWithHostname generates an RFC 2822-compliant Message-Id based on the informational draft "Recommendations for generating Message IDs", it takes an hostname as argument, so that software using this library could use a hostname they know to be unique
func (*Header) MessageID ¶ added in v0.11.2
MessageID parses the Message-ID field. It returns the message identifier, without the angle brackets. If the message doesn't have a Message-ID header field, it returns an empty string.
func (*Header) MsgIDList ¶ added in v0.11.2
MsgIDList parses a list of message identifiers. It returns message identifiers without angle brackets. If the header field is missing, it returns nil.
This can be used on In-Reply-To and References header fields.
func (*Header) SetAddressList ¶
SetAddressList formats the named header field to the provided list of addresses.
This can be used on From, Sender, Reply-To, To, Cc and Bcc header fields.
func (*Header) SetMessageID ¶ added in v0.14.1
SetMessageID sets the Message-ID field. id is the message identifier, without the angle brackets.
func (*Header) SetMsgIDList ¶ added in v0.13.0
SetMsgIDList formats a list of message identifiers. Message identifiers don't include angle brackets.
This can be used on In-Reply-To and References header fields.
func (*Header) SetSubject ¶
SetSubject formats the Subject header field.
type InlineHeader ¶ added in v0.10.0
type InlineHeader struct {
message.Header
}
A InlineHeader represents a message text header.
type InlineWriter ¶ added in v0.10.0
type InlineWriter struct {
// contains filtered or unexported fields
}
InlineWriter writes a mail message's text.
func CreateInlineWriter ¶ added in v0.12.0
func CreateInlineWriter(w io.Writer, header Header) (*InlineWriter, error)
CreateInlineWriter writes a mail header to w. The mail will contain an inline part, allowing to represent the same text in different formats. Attachments cannot be included.
func (*InlineWriter) Close ¶ added in v0.10.0
func (w *InlineWriter) Close() error
Close finishes the InlineWriter.
func (*InlineWriter) CreatePart ¶ added in v0.10.0
func (w *InlineWriter) CreatePart(h InlineHeader) (io.WriteCloser, error)
CreatePart creates a new text part with the provided header. The body of the part should be written to the returned io.WriteCloser.
type Part ¶
type Part struct { Header PartHeader Body io.Reader }
A Part is either a mail text or an attachment. Header is either a InlineHeader or an AttachmentHeader.
type PartHeader ¶
type PartHeader interface { // Add adds the key, value pair to the header. Add(key, value string) // Del deletes the values associated with key. Del(key string) // Get gets the first value associated with the given key. If there are no // values associated with the key, Get returns "". Get(key string) string // Set sets the header entries associated with key to the single element // value. It replaces any existing values associated with key. Set(key, value string) }
A PartHeader is a mail part header. It contains convenience functions to get and set header fields.
type Reader ¶
type Reader struct { Header Header // contains filtered or unexported fields }
A Reader reads a mail message.
Example ¶
package main import ( "io" "io/ioutil" "log" "github.com/emersion/go-message/mail" ) func main() { // Let's assume r is an io.Reader that contains a mail. var r io.Reader // Create a new mail reader mr, err := mail.CreateReader(r) if err != nil { log.Fatal(err) } // Read each mail's part for { p, err := mr.NextPart() if err == io.EOF { break } else if err != nil { log.Fatal(err) } switch h := p.Header.(type) { case *mail.InlineHeader: b, _ := ioutil.ReadAll(p.Body) log.Printf("Got text: %v\n", string(b)) case *mail.AttachmentHeader: filename, _ := h.Filename() log.Printf("Got attachment: %v\n", filename) } } }
Output:
func CreateReader ¶
CreateReader reads a mail header from r and returns a new mail reader.
If the message uses an unknown transfer encoding or charset, CreateReader returns an error that verifies message.IsUnknownCharset, but also returns a Reader that can be used.
func (*Reader) NextPart ¶
NextPart returns the next mail part. If there is no more part, io.EOF is returned as error.
The returned Part.Body must be read completely before the next call to NextPart, otherwise it will be discarded.
If the part uses an unknown transfer encoding or charset, NextPart returns an error that verifies message.IsUnknownCharset, but also returns a Part that can be used.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
A Writer writes a mail message. A mail message contains one or more text parts and zero or more attachments.
Example ¶
package main import ( "bytes" "io" "log" "time" "github.com/emersion/go-message/mail" ) func main() { var b bytes.Buffer from := []*mail.Address{{"Mitsuha Miyamizu", "mitsuha.miyamizu@example.org"}} to := []*mail.Address{{"Taki Tachibana", "taki.tachibana@example.org"}} // Create our mail header var h mail.Header h.SetDate(time.Now()) h.SetAddressList("From", from) h.SetAddressList("To", to) // Create a new mail writer mw, err := mail.CreateWriter(&b, h) if err != nil { log.Fatal(err) } // Create a text part tw, err := mw.CreateInline() if err != nil { log.Fatal(err) } var th mail.InlineHeader th.Set("Content-Type", "text/plain") w, err := tw.CreatePart(th) if err != nil { log.Fatal(err) } io.WriteString(w, "Who are you?") w.Close() tw.Close() // Create an attachment var ah mail.AttachmentHeader ah.Set("Content-Type", "image/jpeg") ah.SetFilename("picture.jpg") w, err = mw.CreateAttachment(ah) if err != nil { log.Fatal(err) } // TODO: write a JPEG file to w w.Close() mw.Close() log.Println(b.String()) }
Output:
func CreateWriter ¶
CreateWriter writes a mail header to w and creates a new Writer.
func (*Writer) CreateAttachment ¶
func (w *Writer) CreateAttachment(h AttachmentHeader) (io.WriteCloser, error)
CreateAttachment creates a new attachment with the provided header. The body of the part should be written to the returned io.WriteCloser.
func (*Writer) CreateInline ¶ added in v0.10.0
func (w *Writer) CreateInline() (*InlineWriter, error)
CreateInline creates a InlineWriter. One or more parts representing the same text in different formats can be written to a InlineWriter.
func (*Writer) CreateSingleInline ¶ added in v0.10.0
func (w *Writer) CreateSingleInline(h InlineHeader) (io.WriteCloser, error)
CreateSingleInline creates a new single text part with the provided header. The body of the part should be written to the returned io.WriteCloser. Only one single text part should be written, use CreateInline if you want multiple text parts.