email

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2020 License: MPL-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package email implements the parsing of email and mime mail messages, and may also be used to create and send email messages.

Index

Constants

View Source
const (
	// MaxHeaderLineLength ...
	MaxHeaderLineLength = 78

	// MaxHeaderTotalLength ...
	MaxHeaderTotalLength = 998
)
View Source
const (
	// MaxBodyLineLength ...
	MaxBodyLineLength = 76
)

Variables

View Source
var ErrHeadersMissingField = errors.New("Message missing header field")

ErrHeadersMissingField ...

Functions

func GenContentID

func GenContentID(filename string) (string, error)

GenContentID creates and returns a Content-ID, without surrounding angle brackets.

func GenMessageID

func GenMessageID() (string, error)

GenMessageID creates and returns a Message-ID, without surrounding angle brackets.

Types

type Header map[string][]string

Header represents the key-value MIME-style pairs in a mail message header. Based on textproto.MIMEHeader and mail.Header.

func NewHeader

func NewHeader(from string, subject string, to ...string) Header

NewHeader returns a Header for the most typical use case: a From address, a Subject, and a variable number of To addresses.

func (Header) Add

func (h Header) Add(key, value string)

Add adds the key, value pair to the header. It appends to any existing values associated with key.

func (Header) AddressList

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

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

func (Header) Bcc

func (h Header) Bcc() []string

Bcc ...

func (Header) Bytes

func (h Header) Bytes() ([]byte, error)

Bytes returns the bytes representing this header. It is a convenience method that calls WriteTo on a buffer, returning its bytes.

func (Header) Cc

func (h Header) Cc() []string

Cc ...

func (Header) ContentDisposition

func (h Header) ContentDisposition() (string, map[string]string, error)

ContentDisposition parses and returns the media disposition, any parameters on it, and an error if there is no content disposition header field.

func (Header) ContentType

func (h Header) ContentType() (string, map[string]string, error)

ContentType parses and returns the content media type, any parameters on it, and an error if there is no content type header field.

func (Header) Date

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

Date parses the Date header field.

func (Header) Del

func (h Header) Del(key string)

Del deletes the values associated with key.

func (Header) From

func (h Header) From() string

From ...

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 "". Get is a convenience method. For more complex queries, access the map directly.

func (Header) IsSet

func (h Header) IsSet(key string) bool

IsSet tests if a key is present in the Header

func (Header) Save

func (h Header) Save() error

Save adds headers for the "Message-Id", "Date", and "MIME-Version", if missing. An error is returned if the Message-Id can not be created.

func (Header) Set

func (h Header) Set(key, value string)

Set sets the header entries associated with key to the single element value. It replaces any existing values associated with key.

func (Header) SetBcc

func (h Header) SetBcc(emails ...string)

SetBcc ...

func (Header) SetCc

func (h Header) SetCc(emails ...string)

SetCc ...

func (Header) SetFrom

func (h Header) SetFrom(email string)

SetFrom ...

func (Header) SetSubject

func (h Header) SetSubject(subject string)

SetSubject ...

func (Header) SetTo

func (h Header) SetTo(emails ...string)

SetTo ...

func (Header) Subject

func (h Header) Subject() string

Subject ...

func (Header) To

func (h Header) To() []string

To ...

func (Header) WriteTo

func (h Header) WriteTo(w io.Writer) (int64, error)

WriteTo writes this header out, including every field except for Bcc.

type Message

type Message struct {
	// Header is this message's key-value MIME-style pairs in its header.
	Header Header

	// Preamble is any text that appears before the first mime multipart,
	// and may only be full in the case where this Message has a Content-Type of "multipart".
	Preamble []byte

	// Epilogue is any text that appears after the last mime multipart,
	// and may only be full in the case where this Message has a Content-Type of "multipart".
	Epilogue []byte

	// Parts is a slice of Messages contained within this Message,
	// and is full in the case where this Message has a Content-Type of "multipart".
	Parts []*Message

	// SubMessage is an encapsulated message, and is full in the case
	// where this Message has a Content-Type of "message".
	SubMessage *Message

	// Body is a byte array of the body of this message, and is full
	// whenever this message doesn't have a Content-Type of "multipart" or "message".
	// The Body is already decoded if the Content-Transfer-Encoding was
	// quoted-printable or base64, and will be re-encoded when written out
	// based on the Content-Type.
	Body []byte
}

Message represents a full email message, or a mime-message (such as a single part in a multipart message). It has fields for the Header and the payload, which may take several forms depending on the Content-Type of this message. If the Content-Type is "message", then the payload will be a SubMessage. If the Content-Type is "multipart", then the payload will be Parts, and optionally the Preamble and Epilogue will be full. If the Content-Type is neither "message" nor "multipart", then the payload will be a Body (decoded if quoted-printable or base64).

func NewMessage

func NewMessage(headers Header, textPlain string, html string, attachments ...*Message) *Message

NewMessage will create a multipart email containing plain text, html, and optionally attachments (create attachments with NewPartAttachment). Example structure with 2 pdf attachments:

  • multipart/mixed
  • * multipart/alternative
  • * * text/plain
  • * * text/html
  • * application/pdf (attachment)
  • * application/pdf (attachment)

func NewMessageWithInlines

func NewMessageWithInlines(headers Header, textPlain string, html string, inlines []*Message, attachments ...*Message) *Message

NewMessageWithInlines will create a multipart email containing plain text, html, and optionally attachments (create attachments with NewPartAttachment), where the html part contains inline parts, such as inline images (create inline parts with NewPartInline). Example structure with 2 inline jpeg's and 2 pdf attachments:

  • multipart/mixed
  • * multipart/alternative
  • * * text/plain
  • * * multipart/related
  • * * * text/html
  • * * * image/jpeg (inline with Content-ID)
  • * * * image/jpeg (inline with Content-ID)
  • * application/pdf (attachment)
  • * application/pdf (attachment)

func NewPartAttachment

func NewPartAttachment(r io.Reader, filename string) (*Message, error)

NewPartAttachment creates an attachment part, using the filename's mime type, and with the reader's content (do not encode, this will happen automatically when needed).

func NewPartAttachmentFromBytes

func NewPartAttachmentFromBytes(raw []byte, filename string) *Message

NewPartAttachmentFromBytes creates an attachment part, using the filename's mime type, and with the bytes as its content (do not encode, this will happen automatically when needed).

func NewPartHTML

func NewPartHTML(html string) *Message

NewPartHTML creates a "text/html" part, with the html string as its content (do not encode, this will happen automatically when needed).

func NewPartInline

func NewPartInline(r io.Reader, filename string, contentID string) (*Message, error)

NewPartInline creates an inline part, using the filename's mime type, specified Content-ID (do not wrap with angle brackets), and with the reader's content (do not encode, this will happen automatically when needed).

func NewPartInlineFromBytes

func NewPartInlineFromBytes(raw []byte, filename string, contentID string) *Message

NewPartInlineFromBytes creates an inline part, using the filename's mime type, specified Content-ID (do not wrap with angle brackets), and with the bytes as its content (do not encode, this will happen automatically when needed).

func NewPartMultipart

func NewPartMultipart(multipartSubType string, parts ...*Message) *Message

NewPartMultipart will create a multipart part, optionally filled with sub-parts. Common values for parameter multipartSubType are: mixed, alternative, related, and report. Example: if "mixed" is passed in as multipartSubType, then a "multipart/mixed" part is created.

func NewPartText

func NewPartText(textPlain string) *Message

NewPartText creates a "text/plain" part, with the text string as its content (do not encode, this will happen automatically when needed).

func ParseMessage

func ParseMessage(r io.Reader) (*Message, error)

ParseMessage parses and returns a Message from an io.Reader containing the raw text of an email message. (If the raw email is a string or []byte, use strings.NewReader() or bytes.NewReader() to create a reader.) Any "quoted-printable" or "base64" encoded bodies will be decoded.

func (*Message) Bytes

func (m *Message) Bytes() ([]byte, error)

Bytes returns the bytes representing this message. It is a convenience method that calls WriteTo on a buffer, returning its bytes.

func (*Message) DeliveryStatusMessageDNS

func (m *Message) DeliveryStatusMessageDNS() (Header, error)

DeliveryStatusMessageDNS returns the message DNS information, or an error if HasDeliveryStatusMessage would return false.

func (*Message) DeliveryStatusRecipientDNS

func (m *Message) DeliveryStatusRecipientDNS() ([]Header, error)

DeliveryStatusRecipientDNS returns the message recipients' DNS information, or an error if HasDeliveryStatusMessage would return false.

func (*Message) HasBody

func (m *Message) HasBody() bool

HasBody returns true if the Content-Type is not "multipart" nor "message"

func (*Message) HasDeliveryStatusMessage

func (m *Message) HasDeliveryStatusMessage() bool

HasDeliveryStatusMessage returns true if this Message has a content type of "message/delivery-status" and has a non-nil SubMessage containing the delivery status information.

func (*Message) HasFeedbackReportMessage

func (m *Message) HasFeedbackReportMessage() bool

HasFeedbackReportMessage returns true if this Message has a content type of "message/feedback-report" and has a non-nil SubMessage.

func (*Message) HasParts

func (m *Message) HasParts() bool

HasParts returns true if the Content-Type is "multipart"

func (*Message) HasSubMessage

func (m *Message) HasSubMessage() bool

HasSubMessage returns true if the Content-Type is "message"

func (*Message) MessagesAll

func (m *Message) MessagesAll() []*Message

MessagesAll will return a slice of Messages, starting with this message, and followed by all messages contained within this message, recursively. This method is similar to Python's email message "walk" function. This method DOES recurse into sub-messages and sub-parts.

func (*Message) MessagesContentTypePrefix

func (m *Message) MessagesContentTypePrefix(contentTypePrefix string) []*Message

MessagesContentTypePrefix will return a slice of all Messages that have this contentTypePrefix, potentially including this message and messages contained within this message. contentTypePrefix can be a prefix ("text"), or a full type ("text/html"). This method DOES recurse into sub-messages and sub-parts.

func (*Message) MessagesFilter

func (m *Message) MessagesFilter(filter func(*Message) bool) []*Message

MessagesFilter will return a slice of all Messages that match this lambda function, potentially including this message and messages contained within this message. This method DOES recurse into sub-messages and sub-parts.

func (*Message) PartsContentTypePrefix

func (m *Message) PartsContentTypePrefix(contentTypePrefix string) []*Message

PartsContentTypePrefix will return a slice of all parts of this message that have this contentTypePrefix. contentTypePrefix can be a prefix ("text"), or a full type ("text/html"). The slice will be empty if this message is not a multipart message. This method does NOT recurse into sub-messages and sub-parts.

func (*Message) PartsFilter

func (m *Message) PartsFilter(filter func(*Message) bool) []*Message

PartsFilter will return a slice of all parts of this message that match this lambda function. The slice will be empty if this message is not a multipart message. This method does NOT recurse into sub-messages and sub-parts.

func (*Message) Payload

func (m *Message) Payload() interface{}

Payload will return the payload of the message, which can only be one the following: Body ([]byte), SubMessage (*Message), or Parts ([]*Message)

func (*Message) Save

func (m *Message) Save() error

Save adds headers for the "Message-Id", "Date", and "MIME-Version", if missing. An error is returned if the Message-Id can not be created.

func (*Message) Send

func (m *Message) Send(smtpAddressPort string, auth smtp.Auth) error

Send this email using the SMTP Address:Port, and optionally any SMTP Auth. Send will call Save() on the message before sending.

func (*Message) WriteTo

func (m *Message) WriteTo(w io.Writer) (int64, error)

WriteTo writes out this Message and its payloads, recursively. Any text bodies will be quoted-printable encoded, and all other bodies will be base64 encoded.

Jump to

Keyboard shortcuts

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