parser

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package parser provides the capabilities for parsing an email io.Reader into an email.Email. The parser can receive options of type `Opt` which alter the parsing process.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Opt

type Opt func(p *Parser)

Opt is a parser option type provided as a closure to add options to a parser default instance instantiated by NewParser. The options are held in opts.go providing closures returning an Opt such as WithHeadersOnly().

func WithCustomAddressFunc

func WithCustomAddressFunc(af func(string) (*mail.Address, error)) Opt

WithCustomAddressFunc allows for the provision of a custom func for parsing an email name/address combination.

func WithCustomAddressesFunc

func WithCustomAddressesFunc(af func(list string) ([]*mail.Address, error)) Opt

WithCustomAddressesFunc allows for the provision of a custom func for parsing lists of email names and addresses from strings.

func WithCustomDateFunc

func WithCustomDateFunc(df func(string) (time.Time, error)) Opt

WithCustomDateFunc allows for the provision of a custom date parsing func.

func WithCustomFileFunc

func WithCustomFileFunc(ff func(*email.File) error) Opt

WithCustomFileFunc allows for the provision of a custom func for reading a file attachment io.Reader. Note that the io.Reader provided by the underlying net/mail package is not concurrent safe. The reader for each file should be drained in turn, otherwise it is likely to be unexpectedly truncated.

Note that all the fields of Parser and email.File are available for custom uses, such as file filtering, file saving or sending files over the network.

func WithHeadersOnly

func WithHeadersOnly() Opt

WithHeadersOnly only parses email headers, which typically provides a substantial speedup in processing.

func WithSaveFilesToDirectory

func WithSaveFilesToDirectory(dir string) Opt

WithSaveFilesToDirectory is an example WithCustomFileFunc showing how to inject a custom-defined func into the parser to save inline and attached files from an email to the supplied directory.

Note that all the public fields in email.Email are available, as are the fields of email.File. For example the message ID of each email could be used to make a directory, and then each file saved in sequence using its file name suffix (if available).

Caution should be used using the filename provided in internet-provided emails, although some cleaning is done by the parsers module -- see Parser.parseFile.

func WithSkipContentTypes added in v0.1.1

func WithSkipContentTypes(skipContentTypes []string) Opt

WithSkipContentTypes allows the user to provide a slice of content types whose email parts will be skipped in processing.

func WithVerbose

func WithVerbose() Opt

WithVerbose is presently a no-op option for testing if options are working as anticipated.

func WithoutAttachments

func WithoutAttachments() Opt

WithoutAttachments skips parsing email attachments, which often provides a speedup in processing.

type Parser

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

func NewParser

func NewParser(options ...Opt) *Parser

NewParser initialises a new Parser. The default parser can be changed using options returning an Opt.

func (*Parser) Parse

func (p *Parser) Parse(r io.Reader) (*email.Email, error)

Parse is the main entry point of letters.

type UnknownContentTypeError

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

UnknownContentTypeError reports an unknown Content Type

func (*UnknownContentTypeError) Error

func (e *UnknownContentTypeError) Error() string

Jump to

Keyboard shortcuts

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