Help us shape the future of Go! Take the 2026 Go Developer Survey

mailparse

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package mailparse produces the "parsed view" of a raw RFC 5322 message — inbound mail and the retained copy of sent outbound mail (decision 9 / Slice 4b-3). It derives two representations server-side: the injection-reduced text an agent feeds to a model by default (prefer the text/plain part, else HTML→text, strip quoted reply chains / forwarded headers, cap the length as a token-stuffing guard), and the decoded text/html part for human display.

It is a CONVENIENCE, not a security control: the raw message is always available, and the security decision is made on structured metadata (the auth verdict + sender provenance, which survive as fields), never on this stripped text. So over-stripping is acceptable — the caller can fall back to raw. Best-effort throughout: a parse failure yields the best text available (possibly empty), never an error.

Index

Constants

View Source
const DefaultMaxBytes = 16 * 1024

DefaultMaxBytes caps the parsed text. Generous enough for real messages, small enough to blunt token-stuffing. Callers may override.

View Source
const MaxHTMLBytes = 1 << 20 // 1 MiB

MaxHTMLBytes caps the display HTML (ParsedView.HTML). Unlike the text view — which is capped tight as a token-stuffing guard for model input — the HTML is for human display, so the bound is generous: just a backstop against a pathological multi-MB part bloating every detail response. raw_message stays the full-fidelity escape hatch.

Variables

This section is empty.

Functions

func ParsedBody

func ParsedBody(raw []byte, maxBytes int) (text string, truncated bool)

ParsedBody extracts the injection-reduced text (see ParsedView.Text). truncated is true when the cap cut content. maxBytes <= 0 uses DefaultMaxBytes. Retained for callers that only need the text; callers wanting HTML use Parse.

Types

type Attachment

type Attachment struct {
	Filename    string // decoded filename (RFC 2047/2231), "" if the part has none
	ContentType string // media type only (e.g. "application/pdf"), params stripped
	// ContentID is the part's Content-ID with the surrounding angle brackets
	// stripped (e.g. "ii_abc@mail.gmail.com"), or "" if the part carries none.
	// This is the key an HTML body's `cid:` reference resolves against, so a
	// renderer can map `<img src="cid:ii_abc@mail.gmail.com">` to this
	// attachment. Present mainly on inline images embedded by mail clients.
	ContentID string
	Data      []byte // decoded bytes (Content-Transfer-Encoding applied)
}

Attachment is one decoded attachment part of a MIME message.

func AttachmentAt

func AttachmentAt(raw []byte, index int) (Attachment, bool)

AttachmentAt returns the attachment at the 0-based index, or false if out of range. It re-walks the message (the caller holds the raw bytes); attachments are typically few, so a per-call walk is fine.

func Attachments

func Attachments(raw []byte) []Attachment

Attachments walks the MIME tree of a raw RFC 5322 message and returns its attachment parts in stable document order (depth-first, as they appear in the wire bytes). Index N from this function is the SAME index used by AttachmentAt and by the message read view — it is the authoritative attachment index.

An "attachment" is a leaf part that carries a filename (in Content-Disposition or the Content-Type name param) OR an explicit `Content-Disposition: attachment`. Body parts (text/plain, text/html with no filename) and multipart containers are NOT attachments. Named inline parts (e.g. cid: images) ARE included — they are real fetchable bytes.

On a parse failure or a non-multipart message with no attachment part, returns an empty slice (never nil-panics).

type ParsedView

type ParsedView struct {
	// Text is the injection-reduced plain text: text/plain preferred (else
	// HTML→text), quoted reply/forward chains stripped, capped at maxBytes.
	Text string
	// Truncated is true when the cap cut Text.
	Truncated bool
	// HTML is the decoded text/html part for display, or "" when the message
	// carries no HTML part. Full fidelity (NOT quote-stripped, unlike Text) and
	// capped at MaxHTMLBytes; raw_message stays the canonical copy.
	HTML string
}

ParsedView is the derived view of a raw RFC 5322 message: the injection-reduced Text an agent feeds a model by default, plus the decoded HTML part for human display.

func Parse

func Parse(raw []byte, maxBytes int) ParsedView

Parse extracts the derived view from a raw RFC 5322 message in a single MIME walk. maxBytes <= 0 uses DefaultMaxBytes (applies to Text only). Best-effort: a parse failure yields the best content available, never an error.

Jump to

Keyboard shortcuts

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