msg

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package msg reads and writes RFC 5322 messages: parsing what arrives (tolerant of the malformed mail spam and old clients produce), composing what lfr sends, and turning HTML into readable text.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compose

func Compose(d Draft) ([]byte, error)

Compose renders d as a MIME message with CRLF line endings: text only, multipart/alternative when HTML is present, multipart/mixed with files.

func Fold

func Fold(raw []byte) []byte

Fold wraps header fields longer than 78 characters at whitespace, so lines with encoded words stay within RFC 2047's 76 characters and every line within RFC 5322 §2.1.1's 78 where a break is possible. Only the top header of raw changes; raw has CRLF line endings.

func FormatAddress

func FormatAddress(name, email string) string

FormatAddress renders "Name <email>".

func HTMLToText

func HTMLToText(src string) string

HTMLToText renders HTML as plain text: blocks become paragraphs, list items get dashes, links keep their target when it differs from their text, and scripts, styles and hidden preheaders disappear.

func NewMessageID

func NewMessageID(domain string) string

NewMessageID makes a unique Message-ID on domain.

func Rewrite

func Rewrite(raw []byte, set [][2]string, drop []string) ([]byte, error)

Rewrite replaces or removes top-level header fields of raw and returns the new message; the body is untouched. Fields in set replace every existing field of that name; names in drop are removed.

func Snippet

func Snippet(text string, n int) string

Snippet is a one-line preview of text.

Types

type Address

type Address struct {
	Name  string `json:"name,omitempty"`
	Email string `json:"email"`
}

Address is a display name and address.

func (Address) String

func (a Address) String() string

type Attachment

type Attachment struct {
	Index int    `json:"index"`
	Name  string `json:"name"`
	Type  string `json:"type"`
	Size  int    `json:"size"`
}

Attachment describes one attachment of a parsed message.

func AttachmentData

func AttachmentData(raw []byte, i int) (Attachment, []byte, bool)

AttachmentData returns the content of attachment i of raw.

type Draft

type Draft struct {
	From       Address
	To, Cc     []Address
	ReplyTo    []Address
	Subject    string
	Date       time.Time
	MessageID  string // without angle brackets; generated when empty
	InReplyTo  string
	References []string
	Header     [][2]string // extra fields, in order (e.g. List-Unsubscribe)
	Text       string
	HTML       string
	Files      []File
}

Draft is a message to compose.

type File

type File struct {
	Name string
	Type string // detected from Name when empty
	Data []byte
}

File is an attachment to compose.

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

Header is the top-level header, keys canonical.

func (Header) Get

func (h Header) Get(key string) string

Get returns the first value of a header field, decoded.

func (Header) Has

func (h Header) Has(key string) bool

Has reports whether the field is present.

func (Header) Values

func (h Header) Values(key string) []string

Values returns every raw value of a field.

type Parsed

type Parsed struct {
	From        Address      `json:"from"`
	To          []Address    `json:"to,omitempty"`
	Cc          []Address    `json:"cc,omitempty"`
	ReplyTo     []Address    `json:"reply_to,omitempty"`
	Subject     string       `json:"subject"`
	Date        time.Time    `json:"date"`
	MessageID   string       `json:"message_id,omitempty"`
	InReplyTo   string       `json:"in_reply_to,omitempty"`
	References  []string     `json:"references,omitempty"`
	Text        string       `json:"text"`
	HTML        string       `json:"html,omitempty"`
	Attachments []Attachment `json:"attachments,omitempty"`
	Header      Header       `json:"-"`
	Parts       []Part       `json:"-"` // every leaf part, for reports and bounces
}

Parsed is the readable content of a message.

func Parse

func Parse(raw []byte) *Parsed

Parse reads raw. It never fails on content: unreadable parts are skipped and a message without a usable structure is read as plain text.

type Part

type Part struct {
	Type        string
	Disposition string
	Name        string
	Body        []byte // decoded
}

Part is one leaf of the MIME tree.

Jump to

Keyboard shortcuts

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