go-message: github.com/emersion/go-message/textproto Index | Examples | Files

package textproto

import "github.com/emersion/go-message/textproto"

Index

Examples

Package Files

header.go multipart.go

func WriteHeader Uses

func WriteHeader(w io.Writer, h Header) error

WriteHeader writes a MIME header to w.

type Header struct {
    // contains filtered or unexported fields
}

A Header represents the key-value pairs in a message header.

The header representation is idempotent: if the header can be read and written, the result will be exactly the same as the original (including whitespace). This is required for e.g. DKIM.

Mutating the header is restricted: the only two allowed operations are inserting a new header field at the top and deleting a header field. This is again necessary for DKIM.

Code:

var h textproto.Header
h.Add("From", "<root@nsa.gov>")
h.Add("To", "<root@gchq.gov.uk>")
h.Set("Subject", "Tonight's dinner")

fmt.Println("From: ", h.Get("From"))
fmt.Println("Has Received: ", h.Has("Received"))

fmt.Println("Header fields:")
fields := h.Fields()
for fields.Next() {
    fmt.Println("  ", fields.Key())
}

func ReadHeader Uses

func ReadHeader(r *bufio.Reader) (Header, error)

ReadHeader reads a MIME header from r. The header is a sequence of possibly continued Key: Value lines ending in a blank line.

func (*Header) Add Uses

func (h *Header) Add(k, v string)

Add adds the key, value pair to the header. It prepends to any existing fields associated with key.

func (*Header) Copy Uses

func (h *Header) Copy() Header

Copy creates an independent copy of the header.

func (*Header) Del Uses

func (h *Header) Del(k string)

Del deletes the values associated with key.

func (*Header) Fields Uses

func (h *Header) Fields() HeaderFields

Fields iterates over all the header fields.

The header may not be mutated while iterating, except using HeaderFields.Del.

func (*Header) FieldsByKey Uses

func (h *Header) FieldsByKey(k string) HeaderFields

FieldsByKey iterates over all fields having the specified key.

The header may not be mutated while iterating, except using HeaderFields.Del.

func (*Header) Get Uses

func (h *Header) Get(k string) string

Get gets the first value associated with the given key. If there are no values associated with the key, Get returns "".

func (*Header) Has Uses

func (h *Header) Has(k string) bool

Has checks whether the header has a field with the specified key.

func (*Header) Len Uses

func (h *Header) Len() int

Len returns the amount of fields in the header.

func (*Header) Set Uses

func (h *Header) Set(k, v string)

Set sets the header fields associated with key to the single field value. It replaces any existing values associated with key.

type HeaderFields Uses

type HeaderFields interface {
    // Next advances to the next header field. It returns true on success, or
    // false if there is no next field.
    Next() (more bool)
    // Key returns the key of the current field.
    Key() string
    // Value returns the value of the current field.
    Value() string
    // Del deletes the current field.
    Del()
}

HeaderFields iterates over header fields. Its cursor starts before the first field of the header. Use Next to advance from field to field.

type MultipartReader Uses

type MultipartReader struct {
    // contains filtered or unexported fields
}

MultipartReader is an iterator over parts in a MIME multipart body. MultipartReader's underlying parser consumes its input as needed. Seeking isn't supported.

func NewMultipartReader Uses

func NewMultipartReader(r io.Reader, boundary string) *MultipartReader

NewMultipartReader creates a new multipart reader reading from r using the given MIME boundary.

The boundary is usually obtained from the "boundary" parameter of the message's "Content-Type" header. Use mime.ParseMediaType to parse such headers.

func (*MultipartReader) NextPart Uses

func (r *MultipartReader) NextPart() (*Part, error)

NextPart returns the next part in the multipart or an error. When there are no more parts, the error io.EOF is returned.

type MultipartWriter Uses

type MultipartWriter struct {
    // contains filtered or unexported fields
}

A MultipartWriter generates multipart messages.

func NewMultipartWriter Uses

func NewMultipartWriter(w io.Writer) *MultipartWriter

NewMultipartWriter returns a new multipart Writer with a random boundary, writing to w.

func (*MultipartWriter) Boundary Uses

func (w *MultipartWriter) Boundary() string

Boundary returns the Writer's boundary.

func (*MultipartWriter) Close Uses

func (w *MultipartWriter) Close() error

Close finishes the multipart message and writes the trailing boundary end line to the output.

func (*MultipartWriter) CreatePart Uses

func (w *MultipartWriter) CreatePart(header Header) (io.Writer, error)

CreatePart creates a new multipart section with the provided header. The body of the part should be written to the returned Writer. After calling CreatePart, any previous part may no longer be written to.

func (*MultipartWriter) SetBoundary Uses

func (w *MultipartWriter) SetBoundary(boundary string) error

SetBoundary overrides the Writer's default randomly-generated boundary separator with an explicit value.

SetBoundary must be called before any parts are created, may only contain certain ASCII characters, and must be non-empty and at most 70 bytes long.

type Part Uses

type Part struct {
    Header Header
    // contains filtered or unexported fields
}

A Part represents a single part in a multipart body.

func (*Part) Close Uses

func (p *Part) Close() error

func (*Part) Read Uses

func (p *Part) Read(d []byte) (n int, err error)

Read reads the body of a part, after its headers and before the next part (if any) begins.

Package textproto imports 10 packages (graph) and is imported by 40 packages. Updated 2019-10-17. Refresh now. Tools for package owners.