Documentation
¶
Overview ¶
Package mail provides transactional email with a fluent message builder, an SMTP driver, and a development log driver.
Index ¶
- Variables
- func Render(t *template.Template, name string, data any) (string, error)
- type AttachmentInfo
- type Encryption
- type Envelope
- type Mailer
- type Message
- func (m *Message) Attach(filename, contentType string, r io.Reader) *Message
- func (m *Message) Bcc(addresses ...string) *Message
- func (m *Message) Cc(addresses ...string) *Message
- func (m *Message) Envelope() Envelope
- func (m *Message) From(address, name string) *Message
- func (m *Message) HTML(body string) *Message
- func (m *Message) HTMLTemplate(t *template.Template, name string, data any) *Message
- func (m *Message) ReplyTo(address string) *Message
- func (m *Message) Subject(subject string) *Message
- func (m *Message) Text(body string) *Message
- func (m *Message) To(addresses ...string) *Message
- type Options
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoRecipients define package-level implementation state. ErrNoRecipients = errors.New("mail: message has no recipients") // ErrNoBody define package-level implementation state. ErrNoBody = errors.New("mail: message has no body") // ErrUnknownDriver define package-level implementation state. ErrUnknownDriver = errors.New("mail: unknown driver") )
Functions ¶
Types ¶
type AttachmentInfo ¶
type AttachmentInfo struct {
// Filename store data used by this type.
Filename string `json:"filename"`
// ContentType store data used by this type.
ContentType string `json:"content_type"`
// Size store data used by this type.
Size int `json:"size"`
}
AttachmentInfo describes an attachment without exposing its content.
type Encryption ¶
type Encryption string
Encryption selects the SMTP transport security.
const ( // EncryptionNone define package-level implementation state. EncryptionNone Encryption = "none" // plain, port 25 // EncryptionTLS define package-level implementation state. EncryptionTLS Encryption = "tls" // implicit TLS, port 465 // EncryptionSTARTTLS define package-level implementation state. EncryptionSTARTTLS Encryption = "starttls" // mandatory STARTTLS, port 587 )
type Envelope ¶
type Envelope struct {
// From store data used by this type.
From string `json:"from,omitempty"`
// FromName store data used by this type.
FromName string `json:"from_name,omitempty"`
// To store data used by this type.
To []string `json:"to,omitempty"`
// Cc store data used by this type.
Cc []string `json:"cc,omitempty"`
// Bcc store data used by this type.
Bcc []string `json:"bcc,omitempty"`
// ReplyTo store data used by this type.
ReplyTo string `json:"reply_to,omitempty"`
// Subject store data used by this type.
Subject string `json:"subject"`
// Text store data used by this type.
Text string `json:"text,omitempty"`
// HTML store data used by this type.
HTML string `json:"html,omitempty"`
// Attachments store data used by this type.
Attachments []AttachmentInfo `json:"attachments,omitempty"`
}
Envelope is a read-only snapshot of a message for inspection tooling such as the devtools mail outbox. Attachment content is never exposed, only its metadata.
type Mailer ¶
type Mailer interface {
// Send define an operation required by this interface.
Send(ctx context.Context, message *Message) error
// Close define an operation required by this interface.
Close() error
}
Mailer sends messages.
type Message ¶
type Message struct {
// contains filtered or unexported fields
}
Message is a fluent mail builder. Builder errors (bad template, failed attachment read) are deferred and surface at Send.
func (*Message) Envelope ¶
Envelope snapshots the message. Slices are copied so later builder calls do not mutate the snapshot.
func (*Message) HTMLTemplate ¶
HTMLTemplate renders the named template with data as the HTML body.
type Options ¶
type Options struct {
// Driver selects the transport: "log" (default, renders messages into
// the logger) or "smtp".
Driver string
// Host store data used by this type.
Host string
Port int // defaults per encryption: 587 starttls, 465 tls, 25 none
// Username store data used by this type.
Username string
// Password store data used by this type.
Password string
Encryption Encryption // defaults to starttls
// FromAddress is the default sender, required for the smtp driver.
FromAddress string
// FromName store data used by this type.
FromName string
Timeout time.Duration // dial and send, defaults to 15s
Logger *slog.Logger // log driver sink, defaults to slog.Default()
}
Options defines an implementation type used by this package.