email

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2019 License: BSD-3-Clause Imports: 14 Imported by: 1

Documentation

Overview

Package email provide a library for working with Internet Message Format as defined by RFC 5322.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Parameter for Text Media Type, RFC 2046 section 4.1.
	ParamNameCharset = []byte("charset") //nolint: gochecknoglobals

	// Parameters for "application/octet-stream", RFC 2046 section 4.5.1.
	ParamNameType    = []byte("type")    //nolint: gochecknoglobals
	ParamNamePadding = []byte("padding") //nolint: gochecknoglobals

	// Parameter for "multipart", RFC 2046 section 5.1.
	ParamNameBoundary = []byte("boundary") //nolint: gochecknoglobals

	// PArameters for "mulitpart/partial", RFC 2046 section 5.2.2.
	ParamNameID     = []byte("id")     //nolint: gochecknoglobals
	ParamNameNumber = []byte("number") //nolint: gochecknoglobals
	ParamNameTotal  = []byte("total")  //nolint: gochecknoglobals
)

List of known parameter name in header field's value.

Functions

func IsValidLocal

func IsValidLocal(local []byte) bool

IsValidLocal will return true if local part contains valid characters. Local part must,

  • start or end without dot character,
  • contains only printable US-ASCII characters, excluding special characters
  • no multiple sequence of dots.

List of special characters,

"(" / ")" / "<" / ">" / "[" / "]" / ":" / ";" / "@" / "\" / "," / "." / DQUOTE

Types

type Body

type Body struct {
	// Parts contains one or more message body.
	Parts []*MIME
	// contains filtered or unexported fields
}

Body represent single or multiple message body parts.

func ParseBody

func ParseBody(raw, boundary []byte) (body *Body, rest []byte, err error)

ParseBody parse the the raw message's body using boundary.

func (*Body) Relaxed

func (body *Body) Relaxed() (out []byte)

Relaxed canonicalize the original body with "relaxed" algorithm as defined in RFC 6376 section 3.4.4. It remove all trailing whitespaces, reduce sequence of whitespaces inside line into single space, and remove all empty line at the end of body. If body is not empty and not end with CRLF, a CRLF is added.

This function is expensive for message with large body, its better if we call it once and store it somewhere.

func (*Body) Simple

func (body *Body) Simple() (out []byte)

Simple canonicalize the original body with "simple" algorithm as defined in RFC 6376 section 3.4.3. Basically, it converts "*CRLF" at the end of body to a single CRLF. If no message body or no trailing CRLF, a CRLF is added.

func (*Body) String

func (body *Body) String() string

String return text representation of Body.

type ContentType

type ContentType struct {
	Top    []byte
	Sub    []byte
	Params []Param
}

ContentType represent MIME header "Content-Type" field.

func ParseContentType

func ParseContentType(raw []byte) (ct *ContentType, err error)

ParseContentType parse content type from raw bytes.

func (*ContentType) GetParamValue

func (ct *ContentType) GetParamValue(name []byte) []byte

GetParamValue return parameter value related to specific name.

func (*ContentType) String

func (ct *ContentType) String() string

String return text representation of this instance.

type Field

type Field struct {
	// Type of field, the numeric representation of field name.
	Type FieldType
	// Name contains "relaxed" canonicalization of field name.
	Name []byte
	// Value contains "relaxed" canonicalization of field value.
	Value []byte

	// ContentType contains unpacked value of field with Name
	// "Content-Type" or nil if still packed.
	ContentType *ContentType
	// contains filtered or unexported fields
}

Field represent field name and value in header.

func ParseField

func ParseField(raw []byte) (field *Field, rest []byte, err error)

ParseField create and initialize Field by parsing a single line message header field from raw input.

If raw input contains multiple lines, the rest of lines will be returned.

On error, it will return nil Field, and rest will contains the beginning of invalid input.

func (*Field) Relaxed

func (field *Field) Relaxed() (out []byte)

Relaxed return the relaxed canonicalization of field name and value.

func (*Field) Simple

func (field *Field) Simple() (out []byte)

Simple return the simple canonicalization of field name and value.

func (*Field) Unpack

func (field *Field) Unpack() (err error)

Unpack the field Value based on field Name.

type FieldType

type FieldType int

FieldType represent numerical identification of field name.

const (
	FieldTypeOptional FieldType = iota
	// The origination date field, RFC 5322 section 3.6.1.
	FieldTypeDate
	// Originator fields, RFC 5322 section 3.6.2.
	FieldTypeFrom
	FieldTypeSender
	FieldTypeReplyTo
	// Destination fields, RFC 5322 section 3.6.3.
	FieldTypeTo
	FieldTypeCC
	FieldTypeBCC
	// Identitication fields, RFC 5322 section 3.6.4.
	FieldTypeMessageID
	FieldTypeInReplyTo
	FieldTypeReferences
	// Informational fields, RFC 5322 section 3.6.5.
	FieldTypeSubject
	FieldTypeComments
	FieldTypeKeywords
	// Resent fields, RFC 5322 section 3.6.6.
	FieldTypeResentDate
	FieldTypeResentFrom
	FieldTypeResentSender
	FieldTypeResentTo
	FieldTypeResentCC
	FieldTypeResentBCC
	FieldTypeResentMessageID
	// Trace fields, RFC 5322 section 3.6.7.
	FieldTypeReturnPath
	FieldTypeReceived

	// MIME header fields, RFC 2045
	FieldTypeMIMEVersion
	FieldTypeContentType
	FieldTypeContentTransferEncoding
	FieldTypeContentID
	FieldTypeContentDescription

	// DKIM Signature, RFC 6376.
	FieldTypeDKIMSignature
)
type Header struct {
	// contains filtered or unexported fields
}

Header represent list of fields in message.

func ParseHeader

func ParseHeader(raw []byte) (hdr *Header, rest []byte, err error)

ParseHeader parse the raw header from top to bottom.

Raw header that start with CRLF indicate an empty header. In this case, it will return nil Header, indicating that no header was parsed, and the leading CRLF is removed on returned "rest".

The raw header may end with optional CRLF, an empty line that separate header from body of message.

On success it will return the rest of raw input (possible message's body) without leading CRLF.

func (*Header) Boundary

func (hdr *Header) Boundary() []byte

Boundary return the message body boundary defined in Content-Type. If no field Content-Type or no boundary it will return nil.

func (*Header) ContentType

func (hdr *Header) ContentType() *ContentType

ContentType return the unpacked value of field "Content-Type", or nil if no field Content-Type exist or there is an error when unpacking.

func (*Header) DKIM

func (hdr *Header) DKIM(n int) (dkimHeader *Header)

DKIM return sub-header of the "n" DKIM-Signature, start from the top. If no DKIM-Signature found it will return nil.

For example, to get the second DKIM-Signature from the top, call it with "n=2", but if no second DKIM-Signature it will return nil.

func (*Header) Filter

func (hdr *Header) Filter(ft FieldType) (fields []*Field)

Filter specific field type. If multiple fields type exist it will return all of them.

func (*Header) PushTop

func (hdr *Header) PushTop(f *Field)

PushTop put the field at the top of header.

func (*Header) Relaxed

func (hdr *Header) Relaxed() []byte

Relaxed canonicalize the header using "relaxed" algorithm and return it.

func (*Header) Simple

func (hdr *Header) Simple() []byte

Simple canonicalize the header using "simple" algorithm.

type MIME

type MIME struct {
	Header  *Header
	Content []byte
}

MIME represent part of message body with their header and content.

func ParseBodyPart

func ParseBodyPart(raw, boundary []byte) (mime *MIME, rest []byte, err error)

ParseBodyPart parse one body part using boundary and return the rest of body.

func (*MIME) String

func (mime *MIME) String() string

String return string representation of MIME object.

type Mailbox

type Mailbox struct {
	Name    []byte
	Local   []byte
	Domain  []byte
	Address []byte
	// contains filtered or unexported fields
}

Mailbox represent an invidual mailbox.

func ParseAddress

func ParseAddress(raw []byte) (mboxes []*Mailbox, err error)

ParseAddress parse raw address into single or multiple mailboxes. Raw address can be a group of address, list of mailbox, or single mailbox.

A group of address have the following syntax,

DisplayName ":" mailbox-list ";" [comment]

List of mailbox (mailbox-list) have following syntax,

mailbox *("," mailbox)

A single mailbox have following syntax,

[DisplayName] ["<"] local "@" domain [">"]

The angle bracket is optional, but both must be provided.

DisplayName, local, and domain can have comment before and/or after it,

[comment] text [comment]

A comment have the following syntax,

"(" text [comment] ")"

func (*Mailbox) String

func (mbox *Mailbox) String() string

String return the text representation of mailbox.

type Message

type Message struct {
	Header        *Header
	Body          *Body
	DKIMSignature *dkim.Signature
	// contains filtered or unexported fields
}

Message represent an unpacked internet message format.

func ParseFile

func ParseFile(inFile string) (msg *Message, rest []byte, err error)

ParseFile parse message from file.

func ParseMessage

func ParseMessage(raw []byte) (msg *Message, rest []byte, err error)

ParseMessage parse the raw message header and body.

func (*Message) CanonBody

func (msg *Message) CanonBody() (body []byte)

func (*Message) CanonHeader

func (msg *Message) CanonHeader(subHeader *Header, dkimField *Field) []byte

CanonHeader generate the canonicalization of sub-header and DKIM-Signature field.

func (*Message) DKIMSign

func (msg *Message) DKIMSign(pk *rsa.PrivateKey, sig *dkim.Signature) (err error)

DKIMSign sign the message using the private key and signature. The only required fields in signature is SDID and Selector, any other required fields that are empty will be initialized with default values.

Upon calling this function, any field values in header and body MUST be already encoded.

func (*Message) DKIMVerify

func (msg *Message) DKIMVerify() (*dkim.Status, error)

DKIMVerify verify the message signature using DKIM.

func (*Message) String

func (msg *Message) String() string

String return the text representation of Message object.

type Param

type Param struct {
	Key    []byte
	Value  []byte
	Quoted bool // Quoted is true if value is contains special characters.
}

Param represent a key-value in slice of bytes.

Directories

Path Synopsis
Package dkim provide a library to parse and create DKIM-Signature header field value, as defined in RFC 6376, DomainKeys Identified Mail (DKIM) Signatures.
Package dkim provide a library to parse and create DKIM-Signature header field value, as defined in RFC 6376, DomainKeys Identified Mail (DKIM) Signatures.
Package maildir provide a library to manage email using maildir format.
Package maildir provide a library to manage email using maildir format.

Jump to

Keyboard shortcuts

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