i18nmail

package
v0.19.3 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2022 License: Apache-2.0 Imports: 26 Imported by: 3

Documentation

Overview

Package i18nmail implements parsing of mail messages.

For the most part, this package follows the syntax as specified by RFC 5322. Notable divergences:

  • Obsolete address formats are not parsed, including addresses with embedded route information.
  • Group addresses are not parsed.
  • The full range of spacing (the CFWS syntax element) is not supported, such as breaking addresses across lines.

Index

Constants

View Source
const HashKeyName = "X-HashOfFullMessage"

HashKeyName is the header key name for the hash

View Source
const (
	MaxWalkDepth = 32
)

MaxWalkDepth is the maximum depth Walk will descend.

Variables

View Source
var (
	// Debugf prints debug logs. Nil walue prints nothing.
	Debugf func(string, ...interface{})

	// Infof prints informative logs. Nil walue prints nothing.
	Infof func(string, ...interface{})

	// CheckEncoding is true if we should check Base64 encodings
	CheckEncoding = true

	// SaveBadInput is true if we should save bad input
	SaveBadInput = false

	// ErrStopWalk shall be returned by the TodoFunc to stop the walk silently.
	ErrStopWalk = errors.New("stop the walk")
)

AddressParser is a mail address parser.

View Source
var ErrHeaderNotPresent = errors.New("mail: header not in message")
View Source
var WordDecoder = &mime.WordDecoder{
	CharsetReader: func(charset string, input io.Reader) (io.Reader, error) {

		enc, err := htmlindex.Get(charset)
		if err != nil {
			return input, err
		}
		return transform.NewReader(input, enc.NewDecoder()), nil
	},
}

WordDecoder decodes mime rords.

Functions

func DecodeHeaders

func DecodeHeaders(hdr map[string][]string) map[string][]string

DecodeHeaders decodes the headers.

func DecodeRFC2047Word

func DecodeRFC2047Word(s string) (string, error)

DecodeRFC2047Word decodes the string as RFC2407.

func HashBytes

func HashBytes(data []byte) string

HashBytes returns a hash (sha1 atm) for the given bytes

func HeadDecode

func HeadDecode(head string) string

HeadDecode decodes mail header encoding (quopri or base64) such as =?iso-8859-2?Q?MEN-261_K=D6BE_k=E1r.pdf?=

func MakeMsgID

func MakeMsgID() string

MakeMsgID creates a new, globally unique message ID, useable as a Message-ID as per RFC822/RFC2822.

func MakeSectionReader

func MakeSectionReader(r io.Reader, threshold int) (*io.SectionReader, error)

MakeSectionReader reads the reader and returns the byte slice.

If the read length is below the threshold, then the bytes are read into memory; otherwise, a temp file is created, and mmap-ed.

func NewB64Decoder

func NewB64Decoder(enc *base64.Encoding, r io.Reader) io.Reader

NewB64Decoder returns a new filtering bae64 decoder.

func NewB64FilterReader

func NewB64FilterReader(r io.Reader) io.Reader

NewB64FilterReader returns a base64 filtering reader.

func NewFilterReader

func NewFilterReader(r io.Reader, okBytes []byte) io.Reader

NewFilterReader returns a reader which silently throws away bytes not in the okBytes slice.

func Walk

func Walk(part MailPart, todo TodoFunc, dontDescend bool) error

Walk over the parts of the email, calling todo on every part.

By default this is recursive, except dontDescend is true.

func WalkMessage

func WalkMessage(msg *mail.Message, todo TodoFunc, dontDescend bool, parent *MailPart) error

WalkMessage walks over the parts of the email, calling todo on every part. The part.Body given to todo is reused, so read if you want to use it!

By default this is recursive, except dontDescend is true.

func WalkMultipart

func WalkMultipart(mp MailPart, todo TodoFunc, dontDescend bool) error

WalkMultipart walks a multipart/ MIME parts, calls todo on every part mp.Body is reused, so read if you want to use it!

By default this is recursive, except dontDescend is true.

Types

type Address

type Address struct {
	Name    string // Proper name; may be empty.
	Address string // user@domain
}

Address represents a single mail address. An address such as "Barry Gibbs <bg@example.com>" is represented as Address{Name: "Barry Gibbs", Address: "bg@example.com"}.

func ParseAddress

func ParseAddress(address string) (*Address, error)

ParseAddress parses a single RFC 5322 address, e.g. "Barry Gibbs <bg@example.com>"

func ParseAddressList

func ParseAddressList(list string) ([]*Address, error)

ParseAddressList parses the given string as a list of addresses.

func (*Address) String

func (a *Address) String() string

String formats the address as a valid RFC 5322 address. If the address's name contains non-ASCII characters the name will be rendered according to RFC 2047.

type DecoderFunc

type DecoderFunc func(io.Reader) io.Reader

DecoderFunc is a type of a decoder (io.Reader wrapper)

type Header map[string][]string

A Header represents the key-value pairs in a mail message header.

func (Header) AddressList

func (h Header) AddressList(key string) ([]*Address, error)

AddressList parses the named header field as a list of addresses.

func (Header) Date

func (h Header) Date() (time.Time, error)

Date parses the Date header field.

func (Header) Decode

func (h Header) Decode(key string) string

Decode returns the named header field as an utf-8 string.

func (Header) Get

func (h Header) Get(key string) string

Get gets the first value associated with the given key. If there are no values associated with the key, Get returns "".

type MailPart

type MailPart struct {
	// Body of the part.
	Body *io.SectionReader
	// MediaType is the parsed media type.
	MediaType map[string]string
	// Header of the mail part.
	Header textproto.MIMEHeader
	// Parent of this part.
	Parent *MailPart
	// ContenType for the part.
	ContentType string
	// Level is the depth level.
	Level int
	// Seq is a sequence number
	Seq int
}

MailPart is part of a mail or multipart message.

func (*MailPart) Spawn

func (mp *MailPart) Spawn() MailPart

Spawn returns a descendant of the MailPart (Level+1, Parent=*mp, next sequence).

func (MailPart) String

func (mp MailPart) String() string

String returns some string representation of the part.

type TodoFunc

type TodoFunc func(mp MailPart) error

TodoFunc is the type of the function called by Walk and WalkMultipart.

Jump to

Keyboard shortcuts

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