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
- Variables
- func GenContentID(filename string) (string, error)
- func GenMessageID() (string, error)
- type Header
- func (h Header) Add(key, value string)
- func (h Header) AddressList(key string) ([]*mail.Address, error)
- func (h Header) Bcc() []string
- func (h Header) Bytes() ([]byte, error)
- func (h Header) Cc() []string
- func (h Header) ContentDisposition() (string, map[string]string, error)
- func (h Header) ContentType() (string, map[string]string, error)
- func (h Header) Date() (time.Time, error)
- func (h Header) Del(key string)
- func (h Header) From() string
- func (h Header) Get(key string) string
- func (h Header) IsSet(key string) bool
- func (h Header) Save() error
- func (h Header) Set(key, value string)
- func (h Header) SetBcc(emails ...string)
- func (h Header) SetCc(emails ...string)
- func (h Header) SetFrom(email string)
- func (h Header) SetSubject(subject string)
- func (h Header) SetTo(emails ...string)
- func (h Header) Subject() string
- func (h Header) To() []string
- func (h Header) WriteTo(w io.Writer) (int64, error)
- type Message
- func NewMessage(headers Header, textPlain string, html string, attachments ...*Message) *Message
- func NewMessageWithInlines(headers Header, textPlain string, html string, inlines []*Message, ...) *Message
- func NewPartAttachment(r io.Reader, filename string) (*Message, error)
- func NewPartAttachmentFromBytes(raw []byte, filename string) *Message
- func NewPartHTML(html string) *Message
- func NewPartInline(r io.Reader, filename string, contentID string) (*Message, error)
- func NewPartInlineFromBytes(raw []byte, filename string, contentID string) *Message
- func NewPartMultipart(multipartSubType string, parts ...*Message) *Message
- func NewPartText(textPlain string) *Message
- func ParseMessage(r io.Reader) (*Message, error)
- func (m *Message) Bytes() ([]byte, error)
- func (m *Message) DeliveryStatusMessageDNS() (Header, error)
- func (m *Message) DeliveryStatusRecipientDNS() ([]Header, error)
- func (m *Message) HasBody() bool
- func (m *Message) HasDeliveryStatusMessage() bool
- func (m *Message) HasFeedbackReportMessage() bool
- func (m *Message) HasParts() bool
- func (m *Message) HasSubMessage() bool
- func (m *Message) MessagesAll() []*Message
- func (m *Message) MessagesContentTypePrefix(contentTypePrefix string) []*Message
- func (m *Message) MessagesFilter(filter func(*Message) bool) []*Message
- func (m *Message) PartsContentTypePrefix(contentTypePrefix string) []*Message
- func (m *Message) PartsFilter(filter func(*Message) bool) []*Message
- func (m *Message) Payload() interface{}
- func (m *Message) Save() error
- func (m *Message) Send(smtpAddressPort string, auth smtp.Auth) error
- func (m *Message) WriteTo(w io.Writer) (int64, error)
Constants ¶
const ( // MaxHeaderLineLength ... MaxHeaderLineLength = 78 // MaxHeaderTotalLength ... MaxHeaderTotalLength = 998 )
const (
// MaxBodyLineLength ...
MaxBodyLineLength = 76
)
Variables ¶
var ErrHeadersMissingField = errors.New("Message missing header field")
ErrHeadersMissingField ...
Functions ¶
func GenContentID ¶
GenContentID creates and returns a Content-ID, without surrounding angle brackets.
func GenMessageID ¶
GenMessageID creates and returns a Message-ID, without surrounding angle brackets.
Types ¶
type Header ¶
Header represents the key-value MIME-style pairs in a mail message header. Based on textproto.MIMEHeader and mail.Header.
func NewHeader ¶
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 ¶
Add adds the key, value pair to the header. It appends to any existing values associated with key.
func (Header) AddressList ¶
AddressList parses the named header field as a list of addresses.
func (Header) Bytes ¶
Bytes returns the bytes representing this header. It is a convenience method that calls WriteTo on a buffer, returning its bytes.
func (Header) ContentDisposition ¶
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 ¶
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) Get ¶
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) Save ¶
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.
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 ¶
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 ¶
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 ¶
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 ¶
NewPartHTML creates a "text/html" part, with the html string as its content (do not encode, this will happen automatically when needed).
func NewPartInline ¶
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 ¶
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 ¶
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 ¶
NewPartText creates a "text/plain" part, with the text string as its content (do not encode, this will happen automatically when needed).
func ParseMessage ¶
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 ¶
Bytes returns the bytes representing this message. It is a convenience method that calls WriteTo on a buffer, returning its bytes.
func (*Message) DeliveryStatusMessageDNS ¶
DeliveryStatusMessageDNS returns the message DNS information, or an error if HasDeliveryStatusMessage would return false.
func (*Message) DeliveryStatusRecipientDNS ¶
DeliveryStatusRecipientDNS returns the message recipients' DNS information, or an error if HasDeliveryStatusMessage would return false.
func (*Message) HasDeliveryStatusMessage ¶
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 ¶
HasFeedbackReportMessage returns true if this Message has a content type of "message/feedback-report" and has a non-nil SubMessage.
func (*Message) HasSubMessage ¶
HasSubMessage returns true if the Content-Type is "message"
func (*Message) MessagesAll ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.