Documentation
¶
Overview ¶
Package mail defines the email-sending interface used by kit consumers: a Mailer that sends a Message, and a Body abstraction so the same message can carry a plain string, raw HTML, a rendered Go template, or (via the mjml subpackage) a rendered MJML document. The smtp subpackage sends over SMTP; keeping the interface here lets application code depend on mail.Mailer without importing an SMTP client or, unless it actually renders MJML, the MJML/wazero runtime.
Queued (asynchronous) sending is not part of this package. Dispatch a mail event through the events package and call Mailer.Send from a listener; see the mail library docs for the pattern.
Index ¶
- Variables
- type Address
- type Attachment
- type Body
- type Content
- type HTMLBody
- type Mailer
- type Memory
- type Message
- func (m *Message) Attachments() []Attachment
- func (m *Message) BccAddresses() []Address
- func (m *Message) CcAddresses() []Address
- func (m *Message) Content(ctx context.Context) (Content, error)
- func (m *Message) Headers() map[string]string
- func (m *Message) Recipients() []Address
- func (m *Message) ReplyToAddress() Address
- func (m *Message) Sender() Address
- func (m *Message) Subject() string
- func (m *Message) ToAddresses() []Address
- func (m *Message) Validate() error
- func (m *Message) WithAttachment(filename string, data []byte, contentType string) *Message
- func (m *Message) WithAttachmentReader(filename string, r io.Reader, contentType string) *Message
- func (m *Message) WithBcc(addresses ...string) *Message
- func (m *Message) WithBccNamed(name, address string) *Message
- func (m *Message) WithBody(b Body) *Message
- func (m *Message) WithCc(addresses ...string) *Message
- func (m *Message) WithCcNamed(name, address string) *Message
- func (m *Message) WithEmbed(filename string, data []byte, contentType string) *Message
- func (m *Message) WithFrom(address string) *Message
- func (m *Message) WithFromNamed(name, address string) *Message
- func (m *Message) WithHTML(s string) *Message
- func (m *Message) WithHeader(key, value string) *Message
- func (m *Message) WithReplyTo(address string) *Message
- func (m *Message) WithSubject(s string) *Message
- func (m *Message) WithText(s string) *Message
- func (m *Message) WithTo(addresses ...string) *Message
- func (m *Message) WithToNamed(name, address string) *Message
- type SentMessage
- type TemplateBody
Constants ¶
This section is empty.
Variables ¶
var ErrNoBody = errors.New("mail: no body")
ErrNoBody is returned by Validate/Send when a message has no Body set.
var ErrNoRecipient = errors.New("mail: no recipient")
ErrNoRecipient is returned by Validate/Send when a message has no To, Cc, or Bcc recipient.
Functions ¶
This section is empty.
Types ¶
type Attachment ¶
type Attachment struct {
Filename string
ContentType string
Inline bool
// contains filtered or unexported fields
}
Attachment is a file attached to, or embedded (inline) in, a Message. Inline attachments are referenced from HTML as cid:<Filename>.
func (Attachment) Reader ¶
func (a Attachment) Reader() io.Reader
Reader returns the attachment content. It is consumed once, when the driver sends the message.
type Body ¶
Body produces message content at send time. Drivers call Render inside their send span, so render errors are wrapped and traced alongside transmission errors.
func Parsed ¶
func Parsed(html *htmltemplate.Template, text *texttemplate.Template, data any) Body
Parsed returns a Body executing pre-parsed templates against data. Either html or text may be nil.
type Content ¶
Content is the rendered message payload. An empty HTML means the message is text-only; an empty Text means it is HTML-only. At least one must be set.
type HTMLBody ¶
type HTMLBody struct {
// contains filtered or unexported fields
}
HTMLBody is a raw-HTML body with an optional hand-written text/plain alternative. There is no automatic HTML-to-text derivation: set WithText explicitly, or leave the message HTML-only.
type Mailer ¶
Mailer sends one fully-built message synchronously.
func NewDiscard ¶
func NewDiscard() Mailer
NewDiscard returns a Mailer that validates and renders each message but never transmits or logs it.
type Memory ¶
type Memory struct {
// contains filtered or unexported fields
}
Memory is a Mailer that renders and captures every message instead of sending it (Laravel's array driver). Safe for concurrent use.
func (*Memory) Messages ¶
func (m *Memory) Messages() []SentMessage
Messages returns everything sent so far.
type Message ¶
type Message struct {
// contains filtered or unexported fields
}
Message is built fluently; construction never fails outright, but Validate (called by drivers before Send) reports missing recipients or body.
func NewMessage ¶
func NewMessage() *Message
NewMessage starts an empty message. WithFrom defaults to the driver's configured sender when left unset.
func (*Message) Attachments ¶
func (m *Message) Attachments() []Attachment
Attachments returns a copy of the attached and embedded files, in the order added.
func (*Message) BccAddresses ¶
BccAddresses returns a copy of the Bcc recipients.
func (*Message) CcAddresses ¶
CcAddresses returns a copy of the Cc recipients.
func (*Message) Recipients ¶
Recipients returns To+Cc+Bcc combined, for span attributes and tests — never logged or traced by address, only by count.
func (*Message) ReplyToAddress ¶
ReplyToAddress returns the configured Reply-To address, or the zero value if unset.
func (*Message) Sender ¶
Sender returns the configured From address; the zero value means "use the driver's default sender".
func (*Message) ToAddresses ¶
ToAddresses returns a copy of the To recipients.
func (*Message) Validate ¶
Validate reports ErrNoRecipient or ErrNoBody. Drivers call it first in Send.
func (*Message) WithAttachment ¶
WithAttachment adds an attachment from memory. contentType may be empty, in which case the driver sniffs it from the filename/content.
func (*Message) WithAttachmentReader ¶
WithAttachmentReader adds an attachment streamed from r; r is read once, at send time.
func (*Message) WithBccNamed ¶
WithBccNamed adds a Bcc recipient with a display name.
func (*Message) WithCcNamed ¶
WithCcNamed adds a Cc recipient with a display name.
func (*Message) WithEmbed ¶
WithEmbed adds an inline resource, referenced from HTML as cid:<filename>.
func (*Message) WithFrom ¶
WithFrom sets the sender address, overriding the driver's configured default.
func (*Message) WithFromNamed ¶
WithFromNamed sets the sender address with a display name.
func (*Message) WithHeader ¶
WithHeader sets a custom top-level header (e.g. "List-Unsubscribe").
func (*Message) WithReplyTo ¶
WithReplyTo sets the Reply-To address.
func (*Message) WithSubject ¶
WithSubject sets the message subject.
func (*Message) WithToNamed ¶
WithToNamed adds a To recipient with a display name.
type SentMessage ¶
type SentMessage struct {
From Address
To, Cc, Bcc []Address
Subject string
Content Content
Attachments []string // filenames only
}
SentMessage is a rendered, captured message.
type TemplateBody ¶
type TemplateBody struct {
// contains filtered or unexported fields
}
TemplateBody renders an html/template as the HTML part, with an optional text/template alternative rendered from the same filesystem.
func Template ¶
func Template(fsys fs.FS, name string, data any) *TemplateBody
Template renders name from fsys with html/template, escaping data per HTML context. Pair it with WithTextTemplate for a text/plain alternative.
func (*TemplateBody) Render ¶
func (b *TemplateBody) Render(context.Context) (Content, error)
Render implements Body.
func (*TemplateBody) WithFuncs ¶
func (b *TemplateBody) WithFuncs(fm htmltemplate.FuncMap) *TemplateBody
WithFuncs adds a FuncMap before parsing the HTML template.
func (*TemplateBody) WithTextTemplate ¶
func (b *TemplateBody) WithTextTemplate(name string) *TemplateBody
WithTextTemplate renders name from the same fsys with text/template as the text/plain alternative.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package mjml renders MJML email bodies as a mail.Body.
|
Package mjml renders MJML email bodies as a mail.Body. |
|
Package smtp implements mail.Mailer over SMTP using github.com/wneessen/go-mail.
|
Package smtp implements mail.Mailer over SMTP using github.com/wneessen/go-mail. |