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

smtp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package smtp provides an omnimail.Sender that delivers through an SMTP server (a relay such as Postfix, a provider's SMTP endpoint, or a local development server such as Mailpit).

It supports STARTTLS (default, required) and implicit TLS (SMTPS), AUTH PLAIN and LOGIN, dial and session timeouts, and context cancellation. Each Send uses its own connection, so a Sender is safe for concurrent use. Only the standard library is used.

s, err := smtp.New(smtp.Config{
    Host:     "email-smtp.us-east-1.amazonaws.com",
    Username: user,
    Password: pass,
})

Index

Constants

View Source
const (
	DefaultDialTimeout = 10 * time.Second
	DefaultTimeout     = 60 * time.Second
)

Default timeouts.

View Source
const ProviderName = "smtp"

ProviderName is reported in SendResult.Provider and Error.Provider.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthMechanism

type AuthMechanism string

AuthMechanism selects the SMTP AUTH mechanism.

const (
	// AuthAuto picks PLAIN when the server offers it, otherwise LOGIN.
	AuthAuto AuthMechanism = ""
	// AuthPlain uses AUTH PLAIN (RFC 4616).
	AuthPlain AuthMechanism = "PLAIN"
	// AuthLogin uses AUTH LOGIN.
	AuthLogin AuthMechanism = "LOGIN"
)

type Config

type Config struct {
	// Host is the server host name (required).
	Host string
	// Port defaults to 587 (StartTLS), 465 (ImplicitTLS) or 25 (Plaintext).
	Port int
	// Security defaults to StartTLS.
	Security Security
	// Username and Password enable SMTP AUTH when Username is set.
	Username string
	Password string
	// Auth selects the mechanism (default AuthAuto).
	Auth AuthMechanism
	// AllowInsecureAuth permits sending credentials over an unencrypted
	// connection. Off by default; enable only for local test servers.
	AllowInsecureAuth bool
	// TLSConfig customizes TLS. ServerName defaults to Host and MinVersion
	// to TLS 1.2.
	TLSConfig *tls.Config
	// LocalName is the EHLO name (default "localhost").
	LocalName string
	// DialTimeout bounds connecting and the TLS handshake (default 10s).
	DialTimeout time.Duration
	// Timeout bounds a whole send session (default 60s). A shorter context
	// deadline takes precedence.
	Timeout time.Duration
	// MessageIDDomain is the right-hand side of generated Message-IDs
	// (default: the From domain).
	MessageIDDomain string
	// Dialer overrides the network dialer (default *net.Dialer).
	Dialer Dialer
}

Config configures a Sender.

type Dialer

type Dialer interface {
	DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

Dialer opens network connections. *net.Dialer implements it.

type Security

type Security int

Security selects how the connection is encrypted.

const (
	// StartTLS connects in plaintext and upgrades with STARTTLS. The upgrade
	// is required: if the server does not offer it, Send fails. Default port
	// 587.
	StartTLS Security = iota
	// ImplicitTLS uses TLS from the first byte (SMTPS). Default port 465.
	ImplicitTLS
	// Plaintext never encrypts. Use only for local relays and development
	// servers. Default port 25.
	Plaintext
)

func (Security) String

func (s Security) String() string

String returns the name of the security mode.

type Sender

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

Sender delivers messages over SMTP. It is safe for concurrent use.

func New

func New(cfg Config) (*Sender, error)

New validates cfg, applies defaults and returns a Sender. It does not connect.

func (*Sender) Send

func (s *Sender) Send(ctx context.Context, msg *omnimail.Message) (*omnimail.SendResult, error)

Send validates msg, assembles it with omnimail.BuildMIME and delivers it in one SMTP session. Failures are returned as *omnimail.Error classified from the SMTP reply code; cancellation of ctx returns an error matching ctx.Err().

Directories

Path Synopsis
Package smtptest provides an in-process SMTP server for tests.
Package smtptest provides an in-process SMTP server for tests.

Jump to

Keyboard shortcuts

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