Documentation ¶
Overview ¶
Package message provides functions for reading and writing email messages, ensuring they are correctly formatted.
Index ¶
- Constants
- Variables
- func From(r io.ReaderAt) (raddr smtp.Address, header textproto.MIMEHeader, rerr error)
- func ReadHeaders(msg *bufio.Reader) ([]byte, error)
- type Address
- type Envelope
- type HeaderWriter
- type Part
- func (p *Part) Header() (textproto.MIMEHeader, error)
- func (p *Part) HeaderReader() io.Reader
- func (p *Part) ParseNextPart() (*Part, error)
- func (p *Part) RawReader() io.Reader
- func (p *Part) Reader() io.Reader
- func (p *Part) SetMessageReaderAt() error
- func (p *Part) SetReaderAt(r io.ReaderAt)
- func (p *Part) String() string
- func (p *Part) Walk() error
- type Writer
Constants ¶
const RFC5322Z = "02 Jan 2006 15:04 -0700"
Timestamp as used in internet mail messages.
Variables ¶
var (
ErrBadContentType = errors.New("bad content-type")
)
var ErrHeaderSeparator = errors.New("no header separator found")
Functions ¶
func From ¶
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.
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 ¶
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 ¶
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 ¶
HeaderReader returns a reader for the header section of this part, including ending bare CRLF.
func (*Part) ParseNextPart ¶
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 ¶
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) SetMessageReaderAt ¶
SetMessageReaderAt sets a reader on p.Message, which must be non-nil.
func (*Part) SetReaderAt ¶
SetReaderAt sets r as reader for this part and all its sub parts, recursively. No reader is set for any Message subpart, see SetMessageReaderAt.
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.