Help us shape the future of Go! Take the 2026 Go Developer Survey

smtp

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(rt *runner.Runtime, sender Sender)

Register binds mail() to sender. The standard library itself is registered separately by the embedding host (stdlib.Register).

func RegisterConfig added in v0.4.0

func RegisterConfig(rt *runner.Runtime, cfg Config)

RegisterConfig binds mail() for cfg: to the configured host when one is named, and to the Unconfigured refusal otherwise. A host calls it after stdlib.Register, whose default is the refusal, so a configured smtp block upgrades mail() in place and its absence changes nothing.

func RegisterSMTP added in v0.2.4

func RegisterSMTP(rt *runner.Runtime)

RegisterSMTP installs the SMTP class binding, so `new SMTP` in a script constructs a script-configured sender.

func SenderContext added in v0.2.4

func SenderContext(ctx context.Context, sender Sender) context.Context

SenderContext binds a Sender into the context. A runtime running with that context constructs `new SMTP` clients that hand their messages to the sender rather than dialing the configured host, which is how a host captures mail (see Memory) without a mail server.

Types

type Config

type Config struct {
	Host     string `yaml:"host" json:"host"`
	Port     int    `yaml:"port" json:"port"`
	Username string `yaml:"username" json:"username"`
	Password string `yaml:"password" json:"password"`
	From     string `yaml:"from" json:"from"`

	// Insecure accepts the STARTTLS certificate without verifying its chain or
	// names. Hosts that present a self-signed certificate, or one carrying no
	// subjectAltName, are unreachable without it.
	Insecure bool `yaml:"insecure" json:"insecure"`
}

Config contains the connection and sender settings for SMTP.

type Memory added in v0.2.4

type Memory struct {
	// contains filtered or unexported fields
}

Memory is a Sender that appends messages to an in-memory queue instead of delivering them. Hosts bind one with SenderContext to capture what scripts send (tests, local runs, dry runs).

func NewMemory added in v0.2.4

func NewMemory() *Memory

NewMemory creates an empty in-memory sender.

func (*Memory) Messages added in v0.2.4

func (m *Memory) Messages() []Message

Messages returns the queued messages in send order.

func (*Memory) Next added in v0.2.4

func (m *Memory) Next() (Message, bool)

Next pops the oldest queued message, reporting whether one was queued.

func (*Memory) Reset added in v0.2.4

func (m *Memory) Reset()

Reset empties the queue.

func (*Memory) Send added in v0.2.4

func (m *Memory) Send(recipient, subject, body string) error

Send queues a message.

type Message added in v0.2.4

type Message struct {
	Recipient string
	Subject   string
	Body      string
}

Message is a queued email.

type SMTP

type SMTP struct {
	// contains filtered or unexported fields
}

SMTP implements the Sender interface using SMTP

func NewSMTP

func NewSMTP(config Config) *SMTP

NewSMTP creates a new SMTP client

func NewSMTPBinding added in v0.2.4

func NewSMTPBinding(ctx context.Context, options any) (*SMTP, error)

NewSMTPBinding is a mail sender configured for one SMTP server. It takes the connection settings as an associative array:

$smtp = new SMTP(array(
	"host"     => "mail.example.com",
	"port"     => 587,
	"username" => "noreply@example.com",
	"password" => "secret",
	"from"     => "Example <noreply@example.com>",
	"insecure" => true,
));

func (*SMTP) Send

func (s *SMTP) Send(ctx context.Context, recipient, subject, body string) (err error)

Send sends an email via SMTP. It takes a context so the delivery is recorded on the trace of the request that asked for it; the runtime injects it, so a script still calls `$smtp->send($to, $subject, $body)`.

func (*SMTP) Sender added in v0.4.0

func (s *SMTP) Sender() Sender

Sender returns the client as the context-free Sender mail() takes. The mail() binding opens the delivery span itself, so this path delivers without opening a second one.

type Sender

type Sender interface {
	Send(recipient, subject, body string) error
}

Sender interface defines methods for sending emails

func Unconfigured added in v0.4.0

func Unconfigured() Sender

Unconfigured is the Sender mail() is bound to when no smtp block is configured: the call fails catchably, naming what is missing, so calling code keeps one spelling and its own log-the-link fallback, and function_exists("mail") answers true either way.

Jump to

Keyboard shortcuts

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