message

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2023 License: MIT Imports: 16 Imported by: 1

Documentation

Overview

Package message provides functions for reading and writing email messages, ensuring they are correctly formatted.

Index

Constants

View Source
const RFC5322Z = "2 Jan 2006 15:04:05 -0700"

Timestamp as used in internet mail messages.

Variables

View Source
var (
	ErrBadContentType = errors.New("bad content-type")
)
View Source
var ErrHeaderSeparator = errors.New("no header separator found")

Functions

func From

func From(r io.ReaderAt) (raddr smtp.Address, header textproto.MIMEHeader, rerr error)

From extracts the address in the From-header.

An RFC5322 message must have a From header. In theory, multiple addresses may be present. In practice zero or multiple From headers may be present. From returns an error if there is not exactly one address. This address can be used for evaluating a DMARC policy against SPF and DKIM results.

func ReadHeaders

func ReadHeaders(msg *bufio.Reader) ([]byte, error)

ReadHeaders returns the headers of a message, ending with a single crlf. Returns ErrHeaderSeparator if no header separator is found.

Types

type Address

type Address struct {
	Name string // Free-form name for display in mail applications.
	User string // Localpart.
	Host string // Domain in ASCII.
}

Address as used in From and To headers.

type Envelope

type Envelope struct {
	Date      time.Time
	Subject   string
	From      []Address
	Sender    []Address
	ReplyTo   []Address
	To        []Address
	CC        []Address
	BCC       []Address
	InReplyTo string
	MessageID string
}

Envelope holds the basic/common message headers as used in IMAP4.

type HeaderWriter

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

HeaderWriter helps create headers, folding to the next line when it would become too large. Useful for creating Received and DKIM-Signature headers.

func (*HeaderWriter) Add

func (w *HeaderWriter) Add(separator string, texts ...string)

Add adds texts, each separated by separator. Individual elements in text are not wrapped.

func (*HeaderWriter) AddWrap

func (w *HeaderWriter) AddWrap(buf []byte)

AddWrap adds data, folding anywhere in the buffer. E.g. for base64 data.

func (*HeaderWriter) Addf

func (w *HeaderWriter) Addf(separator string, format string, args ...any)

Addf formats the string and calls Add.

func (*HeaderWriter) String

func (w *HeaderWriter) String() string

String returns the header in string form, ending with \r\n.

type Part

type Part struct {
	BoundaryOffset int64 // Offset in message where bound starts. -1 for top-level message.
	HeaderOffset   int64 // Offset in message file where header starts.
	BodyOffset     int64 // Offset in message file where body starts.
	EndOffset      int64 // Where body of part ends. Set when part is fully read.
	RawLineCount   int64 // Number of lines in raw, undecoded, body of part. Set when part is fully read.
	DecodedSize    int64 // Number of octets when decoded. If this is a text mediatype, lines ending only in LF are changed end in CRLF and DecodedSize reflects that.

	MediaType               string            // From Content-Type, upper case. E.g. "TEXT". Can be empty because content-type may be absent. In this case, the part may be treated as TEXT/PLAIN.
	MediaSubType            string            // From Content-Type, upper case. E.g. "PLAIN".
	ContentTypeParams       map[string]string // E.g. holds "boundary" for multipart messages. Has lower-case keys, and original case values.
	ContentID               string
	ContentDescription      string
	ContentTransferEncoding string    // In upper case.
	Envelope                *Envelope // Email message headers. Not for non-message parts.

	Parts []Part // Parts if this is a multipart.

	// Only for message/rfc822 and message/global. This part may have a buffer as
	// backing io.ReaderAt, because a message/global can have a non-identity
	// content-transfer-encoding. This part has a nil parent.
	Message *Part
	// contains filtered or unexported fields
}

Part represents a whole mail message, or a part of a multipart message. It is designed to handle IMAP requirements efficiently.

func EnsurePart

func EnsurePart(r io.ReaderAt, size int64) (Part, error)

EnsurePart parses a part as with Parse, but ensures a usable part is always returned, even if error is non-nil. If a parse error occurs, the message is returned as application/octet-stream, and headers can still be read if they were valid.

func Parse

func Parse(r io.ReaderAt) (Part, error)

Parse reads the headers of the mail message and returns a part. A part provides access to decoded and raw contents of a message and its multiple parts.

func (*Part) Header

func (p *Part) Header() (textproto.MIMEHeader, error)

Header returns the parsed header of this part.

func (*Part) HeaderReader

func (p *Part) HeaderReader() io.Reader

HeaderReader returns a reader for the header section of this part, including ending bare CRLF.

func (*Part) ParseNextPart

func (p *Part) ParseNextPart() (*Part, error)

ParseNextPart parses the next (sub)part of this multipart message. ParseNextPart returns io.EOF and a nil part when there are no more parts. Only use for initial parsing of message. Once parsed, use p.Parts.

func (*Part) RawReader

func (p *Part) RawReader() io.Reader

RawReader returns a reader for the raw, undecoded body content. E.g. with quoted-printable or base64 content intact. Fully reading a part helps its parent part find its next part efficiently.

func (*Part) Reader

func (p *Part) Reader() io.Reader

Reader returns a reader for the decoded body content.

func (*Part) SetMessageReaderAt

func (p *Part) SetMessageReaderAt() error

SetMessageReaderAt sets a reader on p.Message, which must be non-nil.

func (*Part) SetReaderAt

func (p *Part) SetReaderAt(r io.ReaderAt)

SetReaderAt sets r as reader for this part and all its sub parts, recursively. No reader is set for any Message subpart, see SetMessageReaderAt.

func (*Part) String

func (p *Part) String() string

String returns a debugging representation of the part.

func (*Part) Walk

func (p *Part) Walk(parent *Part) error

Walk through message, decoding along the way, and collecting mime part offsets and sizes, and line counts.

type Writer

type Writer struct {
	Writer      io.Writer
	HaveHeaders bool
	Has8bit     bool // Whether a byte with the high/8bit has been read. So whether this is 8BITMIME instead of 7BIT.
	Size        int64
	// contains filtered or unexported fields
}

Writer is a write-through helper, collecting properties about the written message.

func (*Writer) Write

func (w *Writer) Write(buf []byte) (int, error)

Write implements io.Writer.

Jump to

Keyboard shortcuts

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