modules/mail/smtp
Package smtp sends email through any SMTP server: Amazon SES, Postmark, Mailgun, Google Workspace, your own server, or Mailpit in development (ADR-0025, ADR-0037).
sender, err := smtp.New("smtp.example.com:587",
smtp.WithAuth(cfg.SMTPUsername, cfg.SMTPPassword),
)
Each Send opens a connection, delivers one message and closes it. Permanent refusals (5xx replies) wrap mail.ErrRejected; everything else is temporary and safe to retry.
Stability: pre-1.0 (ADR-0015).
Constants#
const DefaultTimeout#
const DefaultTimeout = 30 * time.SecondDefaultTimeout bounds one delivery: connecting, authenticating and sending.
Types#
type Option#
type Option interface {
// contains filtered or unexported methods
}An Option configures New.
func WithAuth#
func WithAuth(username string, password config.Secret) OptionWithAuth authenticates with username and password (SMTP AUTH PLAIN), only over an encrypted connection or to a local server.
func WithLocalName#
func WithLocalName(name string) OptionWithLocalName sets the host name sent in EHLO. Default: "localhost".
func WithTLS#
func WithTLS(mode TLSMode) OptionWithTLS sets how the connection is encrypted. Default: TLSStartTLS.
func WithTLSConfig#
func WithTLSConfig(cfg *tls.Config) OptionWithTLSConfig sets the TLS configuration, for example to trust a private certificate authority. Default: system roots, TLS 1.2 or later.
func WithTimeout#
func WithTimeout(d time.Duration) OptionWithTimeout bounds each delivery. Default: DefaultTimeout.
type Sender#
type Sender struct {
// contains filtered or unexported fields
}Sender delivers email over SMTP. It is safe for concurrent use.
func New#
func New(addr string, opts ...Option) (*Sender, error)New returns a sender for the server at addr ("host:port").
func (*Sender) Send#
func (s *Sender) Send(ctx context.Context, m mail.Message) errorSend delivers m. Tags are not sent: SMTP has no standard for them.
type TLSMode#
type TLSMode stringTLSMode is how the connection is encrypted.
const TLSStartTLS, …#
const (
// TLSStartTLS connects in plain text, then requires STARTTLS before
// authenticating or sending. Usually port 587.
TLSStartTLS TLSMode = "starttls"
// TLSImplicit uses TLS from the first byte. Usually port 465.
TLSImplicit TLSMode = "tls"
// TLSNone never encrypts. Use it only for local servers such as Mailpit.
TLSNone TLSMode = "none"
)TLS modes.
func ParseTLSMode#
func ParseTLSMode(s string) (TLSMode, error)ParseTLSMode parses "starttls", "tls" or "none".