Documentation
¶
Overview ¶
Package mail parses MIME messages and sanitizes HTML bodies. It is pure: no storage, network or UI dependencies.
Index ¶
- Constants
- func AddrKey(s string) string
- func Blank(src string) bool
- func Build(o Outgoing, now time.Time) (raw []byte, messageID string, err error)
- func CIDs(src string) []string
- func DecodePart(data []byte, encoding, cs string) ([]byte, error)
- func FormatAddress(name, addr string) string
- func ForwardSubject(s string) string
- func HTMLToText(src string) string
- func JoinDraft(editable, quote, signature string) string
- func MapHTMLText(src string, replace func(string) string) string
- func ParseStructure(bs imap.BodyStructure) (textParts []Attachment, atts []Attachment)
- func QuoteHTML(orig *Parsed) string
- func RemoteImages(safe string) []string
- func ReplaceImages(src string, f func(s string) string) string
- func ReplyRecipients(orig *Parsed, self string, all bool) (to, cc []string)
- func ReplySubject(s string) string
- func Sanitize(src string, inline map[string]Attachment, loadRemote bool) (safe string, blockedRemote int)
- func SanitizeCompose(src string) string
- func SanitizeDraft(src string, cids map[string]bool) string
- func Snippet(text string) string
- func SplitDraft(src string) (editable, quote, signature string)
- func StripMarkers(src string) string
- func ThreadKeys(p *Parsed) (ids []string, subjectKey string)
- type Attachment
- type Outgoing
- type Parsed
Constants ¶
const ( MaxBodyBytes = 2 << 20 MaxMIMEDepth = 20 )
Variables ¶
This section is empty.
Functions ¶
func Blank ¶
Blank reports whether HTML has no content at all: only whitespace and the editor's empty placeholders (p, div, br). An image, a rule or a table is content.
func Build ¶
Build renders o as RFC 5322 bytes. Bcc is never written to the headers (the sender uses it only as envelope recipients). messageID is returned without angle brackets, matching Parsed.MessageID.
func DecodePart ¶
DecodePart decodes a separately fetched MIME part body using its recorded transfer encoding and (for text) charset.
func FormatAddress ¶
FormatAddress renders a display name and address as one header address that net/mail parses back to the same parts. The name stays readable UTF-8 and is quoted (with \ and " escaped) unless it is a plain run of words; so is a local part that is not a dot-atom ("a b"@example.com).
func ForwardSubject ¶
func HTMLToText ¶
HTMLToText converts HTML to plain text: block elements become line breaks, list items get a "- " prefix, links render as "text (url)".
func MapHTMLText ¶ added in v1.3.0
MapHTMLText replaces prose text nodes, leaving code, markup and attributes intact. The callback's result is text: html.Render escapes it, including any model markup.
func ParseStructure ¶
func ParseStructure(bs imap.BodyStructure) (textParts []Attachment, atts []Attachment)
ParseStructure walks an IMAP BODYSTRUCTURE and reports the displayable body parts to fetch and the attachment metadata, with IMAP part paths. ponytail: message/rfc822 children are treated as one attachment, not descended into.
func QuoteHTML ¶
QuoteHTML returns the "on {date}, {from} wrote:" line plus the original in a blockquote.
func RemoteImages ¶
RemoteImages are the http(s) image sources of a sanitized body (kept by Sanitize with loadRemote): its <img> sources and CSS background images, in order, each once.
func ReplaceImages ¶
ReplaceImages returns src with every <img> source s replaced by f(s); an empty result drops the source. It does not sanitize.
func ReplyRecipients ¶
ReplyRecipients picks reply targets: Reply-To if set, else From. With all, the original To/Cc are added to cc. self and duplicates are dropped.
func ReplySubject ¶
func Sanitize ¶
func Sanitize(src string, inline map[string]Attachment, loadRemote bool) (safe string, blockedRemote int)
Sanitize cleans received HTML. inline is keyed by Content-ID. Links become data-href (not navigable); remote images are dropped unless loadRemote. blockedRemote counts remote resources found in the original HTML.
func SanitizeCompose ¶
SanitizeCompose cleans pasted content in the composer: same whitelist, no remote resources. data-godwit survives only as a draft marker: "quote" or "signature".
func SanitizeDraft ¶
SanitizeDraft is SanitizeCompose keeping the images whose src is cid: of a Content-ID in cids (the draft account's signature images); any other cid, data: or remote source is dropped.
func Snippet ¶
Snippet returns the first 200 characters with whitespace collapsed and the invisible characters gone: mail pads its preview text with zero-width and joiner characters between spaces (a preheader), which would fill the line with nothing to see. They are dropped, not made spaces, so none splits a word (a soft hyphen); the joiners only at a word's ends, as inside one they shape it (Persian's non-joiner, an emoji's joiner).
func SplitDraft ¶
SplitDraft splits a sanitized draft body into its editable part, its quote and its signature: the first top-level elements marked "quote" and "signature"; everything else is editable. JoinDraft puts them back together.
func StripMarkers ¶
StripMarkers removes every data-godwit attribute, in any letter case (the parser lowercases names), so marked HTML from a model or another mail becomes ordinary content.
func ThreadKeys ¶
ThreadKeys returns the conversation keys of p: ids is its own Message-ID plus In-Reply-To and References (deduplicated), subjectKey the lowercased subject without Re/Fwd/回复/转发 prefixes and with whitespace collapsed.
Types ¶
type Attachment ¶
type Attachment struct {
Part, Encoding, Charset, Filename, ContentType, ContentID string
Inline bool
Size int
Data []byte
}
Attachment Part is the IMAP-style 1-based dot path ("1", "2.1"). Data is empty when produced by ParseStructure.
type Outgoing ¶
type Outgoing struct {
From, ReplyTo string
To, Cc, Bcc []string
Subject, HTML string
InReplyTo string
References []string
Attachments []Attachment // Data must be filled
// Related are images the HTML may show as cid:<ContentID> (the account's
// signature images): sent inline, in multipart/related, when it does.
Related []Attachment
}
Outgoing is a message to be built. Addresses are "Name <addr>" or bare addresses.