header

package
v0.0.0-...-bb9e38b Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const TimeRFC5322 = "Mon, 2 Jan 2006 15:04:05 -0700"

Variables

This section is empty.

Functions

func CanonicalHeaderKey

func CanonicalHeaderKey(key string) string

CanonicalHeaderKey returns a canonical form of the key whereby the first letter of each word is capitalised

eg, "reply-to" becomes "Reply-To"

if key contains any invalid characters, key is returned unchanged

note this implementation differs to net/textproto CanonicalMIMEHeaderKey which follows RFC 7230

func IsValidHeaderName

func IsValidHeaderName(s string) bool

IsValidHeaderName reports whether a header field name contains only valid characters

A field name MUST be composed of printable US-ASCII characters
(i.e. characters that have values between 33 and 126, inclusive),
except colon (58)

Errata 5918:

...field names should be limited to 77 characters – the field name and
a trailing : – after which the field body can start after FWS on the next line.

func IsValidHeaderValue

func IsValidHeaderValue(s string) bool

IsValidHeaderValue reports whether a header field body contains only valid characters

A field body may be composed of printable US-ASCII characters as well
as the space (SP, ASCII value 32) and horizontal tab (HTAB, ASCII value 9)
characters (together known as the white space characters, WSP)

Types

type Address

type Address struct {
	Field AddressField
	Value string
}

Address represents an Originator or Destination Address header field

usage:

addr := Address{Field: AddressFrom, Value: "alice@secret.com"}
addr := Address{Field: AddressTo, Value: "alice@secret.com, bob@secret.com"}
addr := Address{Field: AddressBcc, Value: "Eavesdrop Eve <eve@secret.com>"}

note: Domain literals and groups are not supported

Syntax:

from            =   "From:" mailbox-list CRLF
sender          =   "Sender:" mailbox CRLF
reply-to        =   "Reply-To:" address-list CRLF
to              =   "To:" address-list CRLF
cc              =   "Cc:" address-list CRLF
bcc             =   "Bcc:" [address-list / CFWS] CRLF
address         =   mailbox / group
addr-spec       =   local-part "@" domain
local-part      =   dot-atom / quoted-string
domain          =   dot-atom / domain-literal
domain-literal  =   [CFWS] "[" *([FWS] dtext) [FWS] "]" [CFWS]
mailbox         =   name-addr / addr-spec
name-addr       =   [display-name] angle-addr
angle-addr      =   [CFWS] "<" addr-spec ">" [CFWS]
group           =   display-name ":" [group-list] ";" [CFWS]
display-name    =   phrase
mailbox-list    =   (mailbox *("," mailbox))
address-list    =   (address *("," address))
group-list      =   mailbox-list / CFWS
phrase          =   1*(word / encoded-word)
word            =   atom / quoted-string
atom            =   [CFWS] 1*atext [CFWS]
dot-atom        =   [CFWS] dot-atom-text [CFWS]
dot-atom-text   =   1*atext *("." 1*atext)
quoted-string   =   [CFWS] DQUOTE *([FWS] qcontent) [FWS] DQUOTE [CFWS]
qcontent        =   qtext / quoted-pair
qtext           =   %d32 / %d33 / %d35-91 / %d93-126

func (Address) Name

func (a Address) Name() string

func (Address) String

func (a Address) String() string

func (Address) Validate

func (a Address) Validate() error

type AddressField

type AddressField string
const (
	AddressFrom    AddressField = "From"
	AddressSender  AddressField = "Sender"
	AddressReplyTo AddressField = "Reply-To"
	AddressTo      AddressField = "To"
	AddressCc      AddressField = "Cc"
	AddressBcc     AddressField = "Bcc"
)

type CustomHeader

type CustomHeader struct {
	FieldName     string
	Value         string
	WordEncodable bool
}

CustomHeader represents an optional field header. WordEncoding can be optionally enabled for an Extension or user defined X-* header field

Syntax:

optional-field  =   field-name ":" unstructured CRLF
field-name      =   1*ftext
ftext           =   %d33-57 / %d59-126
unstructured   =   (*([FWS] VCHAR) *WSP)

ftext: printable ascii except colon

func (CustomHeader) Name

func (u CustomHeader) Name() string

Name returns header name

func (CustomHeader) String

func (u CustomHeader) String() string

func (CustomHeader) Validate

func (u CustomHeader) Validate() error

type Date

type Date time.Time

Date represents the 'Date' header field

Syntax:

date-time       =   [ day-of-week "," ] date time [CFWS]
day-of-week     =   ([FWS] day-name)
day-name        =   "Mon" / "Tue" / "Wed" / "Thu" /
					"Fri" / "Sat" / "Sun"
date            =   day month year
day             =   ([FWS] 1*2DIGIT FWS)
month           =   "Jan" / "Feb" / "Mar" / "Apr" /
					"May" / "Jun" / "Jul" / "Aug" /
					"Sep" / "Oct" / "Nov" / "Dec"
year            =   (FWS 4*DIGIT FWS)
time            =   time-of-day zone
time-of-day     =   hour ":" minute [ ":" second ]
hour            =   2DIGIT
minute          =   2DIGIT
second          =   2DIGIT
zone            =   (FWS ( "+" / "-" ) 4DIGIT)s

func (Date) Name

func (d Date) Name() string

func (Date) String

func (d Date) String() string

func (Date) Validate

func (d Date) Validate() error
type Header interface {
	// Name of header field, MUST be in canonical form
	// eg. 'Reply-To' rather than 'reply-to'
	Name() string
	// Validate against relevant RFCs
	Validate() error
	// String outputs in format complaint with relevant RFCs.
	// The general format is:
	//
	//	Header fields are lines beginning with a field name, followed by a
	//	colon (":"), followed by a field body, and terminated by CRLF
	//
	// In case of validation error, String() should default to user
	// supplied value without any additional formatting
	String() string
}

Header field

type MIMEHeader

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

MIMEHeader represents a Content-[Name] header:

parameter        :=   attribute "=" value
attribute        :=   token
value            :=   token / quoted-string
token            :=   1*<any (US-ASCII) CHAR except SPACE, CTLs, or tspecials>
tspecials        :=   "(" / ")" / "<" / ">" / "@" /
                      "," / ";" / ":" / "\" / <"> /
                      "/" / "[" / "]" / "?" / "="
                      ; Must be in quoted-string to use within parameter values

func NewContentDisposition

func NewContentDisposition(inline bool, filename string, params []MIMEParam) MIMEHeader

MIMEContentDisposition represents the 'Content-Disposition' header

Syntax:

disposition      := "Content-Disposition" ":" disposition-type
                    *(";" disposition-parm)
disposition-type := "inline" / "attachment" / extension-token
                    ; values are not case-sensitive
disposition-parm := filename-parm /
                    creation-date-parm /
                    modification-date-parm /
                    read-date-parm /
                    size-parm /
                    parameter

filename-parm          := "filename" "=" value
creation-date-parm     := "creation-date" "=" quoted-date-time
modification-date-parm := "modification-date" "=" quoted-date-time
read-date-parm         := "read-date" "=" quoted-date-time
size-parm              := "size" "=" 1*DIGIT
quoted-date-time       := quoted-string
                          ; contents MUST be an RFC 822 `date-time'
                          ; numeric timezones (+HHMM or -HHMM) MUST be used

func NewContentID

func NewContentID(val string) MIMEHeader

NewContentID returns 'Content-ID' header:

content-id = "Content-ID" ":" msg-id
msg-id     = [CFWS] "<" id-left "@" id-right ">" [CFWS]

func NewContentTransferEncoding

func NewContentTransferEncoding(val string) MIMEHeader

NewContentTransferEncoding returns 'Content-Transfer-Encoding' header:

encoding   :=  "Content-Transfer-Encoding" ":" mechanism
mechanism  :=  "7bit" / "8bit" / "binary" / "quoted-printable" /
               "base64" / ietf-token / x-token

func NewContentType

func NewContentType(val string, params []MIMEParam) MIMEHeader

NewContentType returns Content-Type header:

content          :=   "Content-Type" ":" type "/" subtype
                      *(";" parameter)
type             :=   discrete-type / composite-type
discrete-type    :=   "text" / "image" / "audio" / "video" /
                      "application" / extension-token
composite-type   :=   "message" / "multipart" / extension-token
subtype          :=   extension-token / iana-token

func NewMIMEHeader

func NewMIMEHeader(name string, val string, params []MIMEParam) MIMEHeader

NewMIMEHeader returns a Content-[name] MIME header

func (MIMEHeader) Name

func (m MIMEHeader) Name() string

func (MIMEHeader) String

func (m MIMEHeader) String() string

func (MIMEHeader) Validate

func (m MIMEHeader) Validate() error

type MIMEParam

type MIMEParam struct {
	Attribute string
	Value     string
}

func NewMIMEParams

func NewMIMEParams(params ...string) []MIMEParam

NewMIMEParams returns mime param array. Input format:

attribute, value, attribute, value...

type MIMEVersion

type MIMEVersion struct{}

MIMEVersion represents the 'MIME-Version' header

default output is MIME-Version: 1.0

func (MIMEVersion) Name

func (m MIMEVersion) Name() string

func (MIMEVersion) String

func (m MIMEVersion) String() string

func (MIMEVersion) Validate

func (m MIMEVersion) Validate() error

type MessageID

type MessageID string

MessageID represents the 'Message-ID' header field.

Usage:

m := MessageID("<2024.12.31@localhost>")

Syntax:

message-id      =   "Message-ID:" msg-id CRLF
msg-id          =   [CFWS] "<" id-left "@" id-right ">" [CFWS]
id-left         =   dot-atom-text
id-right        =   dot-atom-text / no-fold-literal
no-fold-literal =   "[" *dtext "]"
dot-atom-text   =   1*atext *("." 1*atext)
atext           =   ALPHA / DIGIT /
					"!" / "#" / "$" / "%" / "&" / "'" / "*" /
					"+" / "-" / "/" / "=" / "?" / "^" / "_" /
					"`" / "{" /	"|" / "}" / "~"
dtext           =   %d33-90 / %d94-126

dtext: printable ascii excluding "[", "]", or "\"

func (MessageID) Name

func (m MessageID) Name() string

func (MessageID) String

func (m MessageID) String() string

func (MessageID) Validate

func (m MessageID) Validate() error

type Subject

type Subject string

Subject represents the 'Subject' header field

Syntax:

subject         =   "Subject:" unstructured CRLF
unstructured    =   (*([FWS] VCHAR) *WSP)

func (Subject) Name

func (s Subject) Name() string

func (Subject) String

func (s Subject) String() string

func (Subject) Validate

func (s Subject) Validate() error

Validate

since any word encoding will produce printable ascii and satisfy 'unstructured' definition, we check that it can be word encoded instead

Jump to

Keyboard shortcuts

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