message

package
v0.0.0-...-cc74729 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package message defines the abstract view of an email message that Sieve tests and actions consult. Keeping it an interface lets the library stay independent of any particular mail-parsing library.

A net/mail-backed adapter is provided in this package for convenience.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder is a convenience for constructing Messages in tests and small host apps without going through net/mail.

func NewBuilder

func NewBuilder() *Builder

func (*Builder) AddHeader

func (b *Builder) AddHeader(name, value string) *Builder

func (*Builder) AddMIMEPart

func (b *Builder) AddMIMEPart(p MIMEPart) *Builder

AddMIMEPart attaches a pre-built MIME part. Useful for tests that want to exercise the "mime" extension without parsing a multipart blob.

func (*Builder) Build

func (b *Builder) Build() Message

func (*Builder) SetBody

func (b *Builder) SetBody(body []byte) *Builder

func (*Builder) SetEnvelope

func (b *Builder) SetEnvelope(field string, values ...string) *Builder
type Header struct {
	Name  string
	Value string
}

Header is a single name/value pair preserving original case for the name.

type MIMEPart

type MIMEPart interface {
	Header(name string) []string
	AllHeaders() []Header
	Body() io.Reader
	ContentType() string
	IsAttachment() bool
}

MIMEPart is the view of a single MIME body part used by the "mime" Sieve extension (RFC 5703). It exposes part-scope headers and the part body, plus a couple of convenience accessors that mirror what Sieve scripts most commonly want to ask about a part.

func NewMIMEPart

func NewMIMEPart(headers []Header, body []byte) MIMEPart

NewMIMEPart builds a synthetic MIMEPart for tests and host code that already has the headers/body parsed.

type MIMEProvider

type MIMEProvider interface {
	MIMEParts() []MIMEPart
}

MIMEProvider is implemented by Messages that can enumerate their MIME structure. Returned parts are the *child* parts of the message in depth-first order; the root (the message itself) is NOT included — use the Message's own headers for that.

type Message

type Message interface {
	// Header returns all values for a header, in order, in their original
	// form (i.e. with whitespace trimmed but otherwise unchanged).
	Header(name string) []string
	// AllHeaders returns every header in the order it appears in the
	// message.
	AllHeaders() []Header
	// Body returns a reader over the message body (everything after the
	// header/body separator). Each call returns a fresh reader positioned
	// at the start of the body.
	Body() io.Reader
	// Size is the total size of the message in bytes (headers + body).
	Size() int
	// Envelope returns the SMTP envelope values for the named field
	// ("from", "to"). It returns nil if no envelope is associated with
	// the message.
	Envelope(field string) []string
}

Message is the abstract view of a mail message used by Sieve tests. All header lookups are case-insensitive on the name.

func FromNetMail

func FromNetMail(m *mail.Message) Message

FromNetMail wraps a *net/mail.Message. The body is fully read into memory so that Body() and Size() are cheap and repeatable.

func ParseMIME

func ParseMIME(raw []byte) (Message, error)

ParseMIME parses a complete RFC 5322 message (headers + body) and returns a Message that implements MIMEProvider. Non-multipart messages yield zero child parts. Parts are walked depth-first; nested multiparts contribute each of their leaf parts in turn.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL