gomime

package module
v0.0.0-...-7d82a38 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: MIT Imports: 21 Imported by: 30

README

Go Mime Wrapper Library

Provides a parser for MIME messages

Download/Install

Run go get -u github.com/ProtonMail/go-mime, or manually git clone the repository into $GOPATH/src/github.com/ProtonMail/go-mime.

Usage

The library can be used to extract the body and attachments from a MIME message

Example:

printAccepter := gomime.NewMIMEPrinter()
bodyCollector := gomime.NewBodyCollector(printAccepter)
attachmentsCollector := gomime.NewAttachmentsCollector(bodyCollector)
mimeVisitor := gomime.NewMimeVisitor(attachmentsCollector)
err := gomime.VisitAll(bytes.NewReader(mmBodyData), h, mimeVisitor)
attachments := attachmentsCollector.GetAttachments(),
attachmentsHeaders :=	attachmentsCollector.GetAttHeaders()
bodyContent, bodyMimeType := bodyCollector.GetBody()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBadUTF7 = errors.New("utf7: bad utf-7 encoding")

ErrBadUTF7 is returned to indicate the invalid modified UTF-7 encoding.

Functions

func DecodeCharset

func DecodeCharset(original []byte, mediaType string, contentTypeParams map[string]string) ([]byte, error)

DecodeCharset decodes the orginal using content type parameters. When charset missing it checks the content is utf8-valid.

func DecodeContentEncoding

func DecodeContentEncoding(r io.Reader, contentEncoding string) (d io.Reader)

DecodeContentEncoding wraps the reader with decoder based on content encoding

func DecodeHeader

func DecodeHeader(raw string) (decoded string, err error)

DecodeHeader if needed. Returns error if raw contains non-utf8 characters

func EncodeHeader

func EncodeHeader(s string) string

EncodeHeader using quoted printable and utf8

func GetAllChildParts

func GetAllChildParts(part io.Reader, h textproto.MIMEHeader) (parts []io.Reader, headers []textproto.MIMEHeader, err error)

func GetMultipartParts

func GetMultipartParts(r io.Reader, params map[string]string) (parts []io.Reader, headers []textproto.MIMEHeader, err error)

func GetRawMimePart

func GetRawMimePart(rawdata io.Reader, boundary string) (io.Reader, io.Reader)

func IsLeaf

func IsLeaf(h textproto.MIMEHeader) bool

func NewUtf7Decoder

func NewUtf7Decoder() *encoding.Decoder

NewUtf7Decoder return decoder for utf7

func VisitAll

func VisitAll(part io.Reader, h textproto.MIMEHeader, accepter VisitAcceptor) (err error)

Types

type AttachmentsCollector

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

func NewAttachmentsCollector

func NewAttachmentsCollector(targetAccepter VisitAcceptor) *AttachmentsCollector

func (*AttachmentsCollector) Accept

func (ac *AttachmentsCollector) Accept(partReader io.Reader, header textproto.MIMEHeader, hasPlainSibling bool, isFirst, isLast bool) (err error)

func (AttachmentsCollector) GetAttHeaders

func (ac AttachmentsCollector) GetAttHeaders() []string

func (AttachmentsCollector) GetAttachments

func (ac AttachmentsCollector) GetAttachments() []string

type BodyCollector

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

func NewBodyCollector

func NewBodyCollector(targetAccepter VisitAcceptor) *BodyCollector

func (*BodyCollector) Accept

func (bc *BodyCollector) Accept(partReader io.Reader, header textproto.MIMEHeader, hasPlainSibling bool, isFirst, isLast bool) (err error)

func (*BodyCollector) GetBody

func (bc *BodyCollector) GetBody() (string, string)

func (*BodyCollector) GetHeaders

func (bc *BodyCollector) GetHeaders() string

type MIMEPrinter

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

func NewMIMEPrinter

func NewMIMEPrinter() (pd *MIMEPrinter)

func (*MIMEPrinter) Accept

func (pd *MIMEPrinter) Accept(partReader io.Reader, header textproto.MIMEHeader, hasPlainSibling bool, isFirst, isLast bool) (err error)

func (*MIMEPrinter) String

func (pd *MIMEPrinter) String() string

type MimeVisitor

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

MIMEVisitor is main object to parse (visit) and process (accept) all parts of MIME message

func NewMimeVisitor

func NewMimeVisitor(targetAccepter VisitAcceptor) *MimeVisitor

NewMIMEVisitor initialiazed with acceptor

func (*MimeVisitor) Accept

func (mv *MimeVisitor) Accept(part io.Reader, h textproto.MIMEHeader, hasPlainSibling bool, isFirst, isLast bool) (err error)

Accept reads part recursively if needed hasPlainSibling is there when acceptor want to check alternatives

type PlainTextCollector

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

func NewPlainTextCollector

func NewPlainTextCollector(targetAccepter VisitAcceptor) *PlainTextCollector

func (*PlainTextCollector) Accept

func (ptc *PlainTextCollector) Accept(partReader io.Reader, header textproto.MIMEHeader, hasPlainSibling bool, isFirst, isLast bool) (err error)

func (PlainTextCollector) GetPlainText

func (ptc PlainTextCollector) GetPlainText() string

type VisitAcceptor

type VisitAcceptor interface {
	Accept(partReader io.Reader, header textproto.MIMEHeader, hasPlainSibling bool, isFirst, isLast bool) (err error)
}

VisitAcceptor decidest what to do with part which is processed It is used by MIMEVisitor

Jump to

Keyboard shortcuts

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