Documentation
¶
Overview ¶
Package mail provides message construction, MIME attachment rendering, and SMTP sending helpers.
Index ¶
- Variables
- func QuickSend(ctx context.Context, account Account, opts ...QuickOption) error
- func Send(ctx context.Context, host string, port int, message *Message, ...) error
- func SendAccountHTML(ctx context.Context, account Account, to []string, subject string, html string, ...) error
- func SendAccountText(ctx context.Context, account Account, to []string, subject string, text string, ...) error
- func SendHTML(ctx context.Context, host string, port int, from string, to []string, ...) error
- func SendText(ctx context.Context, host string, port int, from string, to []string, ...) error
- type Account
- type Address
- type Attachment
- func NewAttachment(name string, content []byte, contentType ContentType) (Attachment, error)
- func NewAttachmentFile(path string) (Attachment, error)
- func NewAttachmentReader(name string, size int64, contentType ContentType, ...) (Attachment, error)
- func NewInline(name, contentID string, content []byte, contentType ContentType) (Attachment, error)
- func NewInlineFile(path, contentID string) (Attachment, error)
- func NewInlineReader(name string, contentID string, size int64, contentType ContentType, ...) (Attachment, error)
- type BoundaryGenerator
- type Charset
- type Client
- type ClientOption
- func WithAllowPlainAuth(allow bool) ClientOption
- func WithAuth(username, password string) ClientOption
- func WithDialContext(dial DialContextFunc) ClientOption
- func WithLocalName(name string) ClientOption
- func WithSMTPAuth(auth smtp.Auth) ClientOption
- func WithSenderProvider(provider SenderProvider) ClientOption
- func WithTLSConfig(config *tls.Config) ClientOption
- func WithTLSPolicy(policy TLSPolicy) ClientOption
- func WithTimeout(timeout time.Duration) ClientOption
- type Config
- type ContentType
- type DialContextFunc
- type Encoding
- type Header
- type Message
- type MessageOption
- func WithAttachment(name string, content []byte, contentType ContentType) MessageOption
- func WithAttachmentFile(path string) MessageOption
- func WithAttachmentReader(name string, size int64, contentType ContentType, ...) MessageOption
- func WithBcc(addresses ...string) MessageOption
- func WithBoundaryGenerator(generator BoundaryGenerator) MessageOption
- func WithCc(addresses ...string) MessageOption
- func WithCharset(charset Charset) MessageOption
- func WithDate(t time.Time) MessageOption
- func WithEncoding(encoding Encoding) MessageOption
- func WithEnvelopeFrom(address string) MessageOption
- func WithFrom(address string) MessageOption
- func WithFromAddress(addr *Address) MessageOption
- func WithHTML(html string) MessageOption
- func WithHeader(name string, values ...string) MessageOption
- func WithInline(name, contentID string, content []byte, contentType ContentType) MessageOption
- func WithInlineFile(path, contentID string) MessageOption
- func WithInlineReader(name string, contentID string, size int64, contentType ContentType, ...) MessageOption
- func WithMaxAttachmentBytes(maxBytes int64) MessageOption
- func WithMessageID(id string) MessageOption
- func WithReplyTo(addresses ...string) MessageOption
- func WithSubject(subject string) MessageOption
- func WithText(text string) MessageOption
- func WithTo(addresses ...string) MessageOption
- type QuickOption
- type SendCloser
- type Sender
- type SenderFunc
- type SenderProvider
- type TLSPolicy
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidAddress is returned when an email address cannot be parsed or validated. ErrInvalidAddress error = &sentinel{code: knifer.ErrCodeInvalidInput, msg: "mail: invalid address"} // ErrInvalidHeader is returned when a header name or value is not safe for SMTP/MIME output. ErrInvalidHeader error = &sentinel{code: knifer.ErrCodeInvalidInput, msg: "mail: invalid header"} // ErrMissingFrom is returned when a message has no From address. ErrMissingFrom error = &sentinel{code: knifer.ErrCodeInvalidInput, msg: "mail: missing from address"} // ErrMissingRecipient is returned when a message has no To, Cc, or Bcc recipient. ErrMissingRecipient error = &sentinel{code: knifer.ErrCodeInvalidInput, msg: "mail: missing recipient"} // ErrMissingBody is returned when a message has no body content. ErrMissingBody error = &sentinel{code: knifer.ErrCodeInvalidInput, msg: "mail: missing body"} // ErrTLSRequired is returned when the configured security policy requires TLS but TLS is unavailable. ErrTLSRequired error = &sentinel{code: knifer.ErrCodeUnsupported, msg: "mail: tls required"} // ErrPlainAuth is returned when SMTP AUTH would be sent over a plaintext connection. ErrPlainAuth error = &sentinel{code: knifer.ErrCodeUnsupported, msg: "mail: plaintext auth disabled"} // ErrAttachmentTooLarge is returned when an attachment exceeds the configured size limit. ErrAttachmentTooLarge error = &sentinel{code: knifer.ErrCodeInvalidInput, msg: "mail: attachment too large"} )
Functions ¶
func QuickSend ¶
func QuickSend(ctx context.Context, account Account, opts ...QuickOption) error
QuickSend creates and sends a message using account defaults plus quick options.
func SendAccountHTML ¶
func SendAccountHTML( ctx context.Context, account Account, to []string, subject string, html string, opts ...QuickOption, ) error
SendAccountHTML creates and sends an HTML message using account defaults.
func SendAccountText ¶
func SendAccountText( ctx context.Context, account Account, to []string, subject string, text string, opts ...QuickOption, ) error
SendAccountText creates and sends a plain text message using account defaults.
Types ¶
type Account ¶
type Account struct {
Host string
Port int
Username string
Password string
Auth smtp.Auth
From string
FromName string
TLSConfig *tls.Config
TLSPolicy TLSPolicy
AllowPlainAuth bool
Timeout time.Duration
LocalName string
}
Account stores SMTP server, authentication, and default sender settings.
type Address ¶
Address is an RFC 5322 mailbox address.
func NewAddress ¶
NewAddress validates and returns a mailbox address.
func ParseAddress ¶
ParseAddress parses a single mailbox address.
func ParseAddressList ¶
ParseAddressList parses a comma-separated mailbox address list.
type Attachment ¶
type Attachment struct {
Name string
ContentType ContentType
ContentID string
Encoding Encoding
Size int64
Open func() (io.ReadCloser, error)
// contains filtered or unexported fields
}
Attachment is a MIME file part.
func NewAttachment ¶
func NewAttachment(name string, content []byte, contentType ContentType) (Attachment, error)
NewAttachment creates an attachment from bytes.
func NewAttachmentFile ¶
func NewAttachmentFile(path string) (Attachment, error)
NewAttachmentFile creates an attachment loaded lazily from path.
func NewAttachmentReader ¶
func NewAttachmentReader(name string, size int64, contentType ContentType, open func() (io.ReadCloser, error)) (Attachment, error)
NewAttachmentReader creates an attachment from a reader opener.
func NewInline ¶
func NewInline(name, contentID string, content []byte, contentType ContentType) (Attachment, error)
NewInline creates an inline attachment from bytes with a Content-ID.
func NewInlineFile ¶
func NewInlineFile(path, contentID string) (Attachment, error)
NewInlineFile creates an inline attachment loaded lazily from path with a Content-ID.
func NewInlineReader ¶
func NewInlineReader( name string, contentID string, size int64, contentType ContentType, open func() (io.ReadCloser, error), ) (Attachment, error)
NewInlineReader creates an inline attachment from a reader opener with a Content-ID.
type BoundaryGenerator ¶
BoundaryGenerator creates MIME multipart boundaries.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client sends messages through SMTP.
func NewClient ¶
func NewClient(host string, port int, opts ...ClientOption) (*Client, error)
NewClient creates an SMTP client.
type ClientOption ¶
ClientOption customizes Client construction.
func WithAllowPlainAuth ¶
func WithAllowPlainAuth(allow bool) ClientOption
WithAllowPlainAuth permits SMTP AUTH without TLS. Prefer TLS instead.
func WithAuth ¶
func WithAuth(username, password string) ClientOption
WithAuth sets SMTP username and password.
func WithDialContext ¶
func WithDialContext(dial DialContextFunc) ClientOption
WithDialContext sets the network dialer.
func WithLocalName ¶
func WithLocalName(name string) ClientOption
WithLocalName sets the HELO/EHLO local name.
func WithSMTPAuth ¶
func WithSMTPAuth(auth smtp.Auth) ClientOption
WithSMTPAuth sets a custom SMTP authentication mechanism.
func WithSenderProvider ¶
func WithSenderProvider(provider SenderProvider) ClientOption
WithSenderProvider sets a custom sender provider, primarily for deterministic tests.
func WithTLSConfig ¶
func WithTLSConfig(config *tls.Config) ClientOption
WithTLSConfig sets the TLS configuration. The value is cloned.
func WithTLSPolicy ¶
func WithTLSPolicy(policy TLSPolicy) ClientOption
WithTLSPolicy sets SMTP TLS behavior.
func WithTimeout ¶
func WithTimeout(timeout time.Duration) ClientOption
WithTimeout sets a client-wide operation timeout.
type Config ¶
type Config struct {
Host string
Port int
Username string
Password string
Auth smtp.Auth
LocalName string
TLSConfig *tls.Config
TLSPolicy TLSPolicy
AllowPlainAuth bool
Timeout time.Duration
DialContext DialContextFunc
}
Config configures SMTP delivery.
type ContentType ¶
type ContentType string
ContentType identifies a MIME media type.
const ( // TypeTextPlain is plain text content. TypeTextPlain ContentType = "text/plain" // TypeTextHTML is HTML content. TypeTextHTML ContentType = "text/html" // TypeApplicationOctetStream is generic binary content. TypeApplicationOctetStream ContentType = "application/octet-stream" )
func (ContentType) String ¶
func (c ContentType) String() string
type DialContextFunc ¶
DialContextFunc dials an SMTP server.
type Encoding ¶
type Encoding string
Encoding identifies a Content-Transfer-Encoding value.
const ( // EncodingBase64 encodes content using RFC 2045 base64. EncodingBase64 Encoding = "base64" // EncodingQuotedPrintable encodes content using quoted-printable. EncodingQuotedPrintable Encoding = "quoted-printable" // Encoding7Bit marks content as 7bit. Encoding7Bit Encoding = "7bit" // Encoding8Bit marks content as 8bit. Encoding8Bit Encoding = "8bit" )
type Header ¶
type Header struct {
// contains filtered or unexported fields
}
Header stores message headers in insertion order.
type Message ¶
type Message struct {
From *Address
EnvelopeFrom string
To []*Address
Cc []*Address
Bcc []*Address
ReplyTo []*Address
Subject string
Text string
HTML string
Headers Header
Attachments []Attachment
Inlines []Attachment
Date time.Time
MessageID string
Charset Charset
Encoding Encoding
// contains filtered or unexported fields
}
Message is an email message with text, HTML, inline files, and attachments.
func NewMessage ¶
func NewMessage(opts ...MessageOption) (*Message, error)
NewMessage creates and validates an email message.
func (*Message) Recipients ¶
Recipients returns all SMTP envelope recipients, including Bcc.
type MessageOption ¶
MessageOption customizes message construction.
func WithAttachment ¶
func WithAttachment(name string, content []byte, contentType ContentType) MessageOption
WithAttachment appends an attachment from bytes.
func WithAttachmentFile ¶
func WithAttachmentFile(path string) MessageOption
WithAttachmentFile appends an attachment loaded lazily from path.
func WithAttachmentReader ¶
func WithAttachmentReader(name string, size int64, contentType ContentType, open func() (io.ReadCloser, error)) MessageOption
WithAttachmentReader appends an attachment from a reader opener.
func WithBoundaryGenerator ¶
func WithBoundaryGenerator(generator BoundaryGenerator) MessageOption
WithBoundaryGenerator injects the MIME boundary generator.
func WithCharset ¶
func WithCharset(charset Charset) MessageOption
WithCharset sets the charset used for message text parts and encoded headers.
func WithEncoding ¶
func WithEncoding(encoding Encoding) MessageOption
WithEncoding sets the transfer encoding used for text parts.
func WithEnvelopeFrom ¶
func WithEnvelopeFrom(address string) MessageOption
WithEnvelopeFrom sets the SMTP envelope sender for MAIL FROM.
func WithFromAddress ¶
func WithFromAddress(addr *Address) MessageOption
WithFromAddress sets the From address.
func WithHeader ¶
func WithHeader(name string, values ...string) MessageOption
WithHeader appends an additional header.
func WithInline ¶
func WithInline(name, contentID string, content []byte, contentType ContentType) MessageOption
WithInline appends an inline file from bytes.
func WithInlineFile ¶
func WithInlineFile(path, contentID string) MessageOption
WithInlineFile appends an inline attachment loaded lazily from path with a Content-ID.
func WithInlineReader ¶
func WithInlineReader( name string, contentID string, size int64, contentType ContentType, open func() (io.ReadCloser, error), ) MessageOption
WithInlineReader appends an inline file from a reader opener with a Content-ID.
func WithMaxAttachmentBytes ¶
func WithMaxAttachmentBytes(maxBytes int64) MessageOption
WithMaxAttachmentBytes sets the per-attachment size limit. A non-positive value disables the limit.
func WithMessageID ¶
func WithMessageID(id string) MessageOption
WithMessageID sets the Message-ID header without angle brackets.
func WithReplyTo ¶
func WithReplyTo(addresses ...string) MessageOption
WithReplyTo appends Reply-To addresses.
func WithSubject ¶
func WithSubject(subject string) MessageOption
WithSubject sets the Subject header.
type QuickOption ¶
type QuickOption func(*quickConfig) error
QuickOption customizes account-based quick send helpers.
func WithQuickClientOptions ¶
func WithQuickClientOptions(opts ...ClientOption) QuickOption
WithQuickClientOptions appends client options used by QuickSend and account helpers.
func WithQuickMessageOptions ¶
func WithQuickMessageOptions(opts ...MessageOption) QuickOption
WithQuickMessageOptions appends message options used by QuickSend and account helpers.
type SendCloser ¶
SendCloser sends multiple messages through a reusable SMTP connection.
type SenderFunc ¶
SenderFunc adapts a function into Sender.
type SenderProvider ¶
SenderProvider creates a sender for a client configuration.
type TLSPolicy ¶
type TLSPolicy int
TLSPolicy controls SMTP transport security.
const ( // TLSPolicyUnknown uses the package default, currently mandatory STARTTLS. TLSPolicyUnknown TLSPolicy = iota // TLSMandatoryStartTLS requires STARTTLS before SMTP AUTH or DATA. TLSMandatoryStartTLS // TLSImplicit uses implicit TLS from the initial connection. TLSImplicit // TLSOpportunisticStartTLS upgrades when STARTTLS is advertised. TLSOpportunisticStartTLS // TLSNone disables TLS. AUTH remains disabled unless AllowPlainAuth is set. TLSNone )