smtp

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 17 Imported by: 1

Documentation

Overview

Package smtp sends email through any SMTP submission server, so the email provider (SendGrid, ZeptoMail, a self-hosted server, …) is configuration: host, port, username, password and the sender.

Port 465 is TLS from the first byte; any other port upgrades with STARTTLS when the server offers it, and must when credentials are sent to a host other than loopback. Each Send opens its own connection, so a Sender is safe for concurrent use. No error carries the password.

Index

Constants

View Source
const DefaultPort = 587

DefaultPort is the submission port used when Config.Port is 0.

View Source
const DefaultTimeout = 30 * time.Second

DefaultTimeout bounds one Send or CheckHealth when Config.Timeout is 0.

Variables

This section is empty.

Functions

func ParseMailbox

func ParseMailbox(s string) (*mail.Address, error)

ParseMailbox parses one RFC 5322 mailbox, refusing line breaks.

Types

type Config

type Config struct {
	Host string
	// Port is 465 for implicit TLS, else STARTTLS; 0 is DefaultPort.
	Port int
	// Username and Password authenticate (PLAIN, else LOGIN); an empty
	// Username sends without authenticating.
	Username string
	Password string
	// From is the default sender, one RFC 5322 mailbox ("Name <a@b.example>"
	// or "a@b.example"); a Message's From replaces it.
	From string
	// Timeout bounds one Send or CheckHealth; 0 is DefaultTimeout.
	Timeout time.Duration
	// TLS configures the TLS client (a private CA, a test server); nil uses
	// the system roots. Its ServerName defaults to Host.
	TLS *tls.Config
}

Config is an SMTP submission server and the default sender.

type Message

type Message struct {
	// From replaces Config.From for this message.
	From *mail.Address
	// To is one RFC 5322 mailbox.
	To      string
	Subject string
	Text    string
	HTML    string
}

Message is one email to one recipient. It needs a subject and a text or HTML body (both send multipart/alternative).

type Sender

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

Sender delivers Messages through one SMTP server.

func New

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

New validates cfg and returns its Sender. It does not connect.

func (*Sender) CheckHealth

func (s *Sender) CheckHealth(ctx context.Context) error

CheckHealth connects, negotiates TLS and authenticates, then quits without sending: nil when the server would accept this Sender's mail.

func (*Sender) From

func (s *Sender) From() *mail.Address

From is the default sender, or nil when Config.From was empty.

func (*Sender) Send

func (s *Sender) Send(ctx context.Context, m Message) error

Send delivers m. An error from the server wraps its *textproto.Error, whose Code tells a temporary (4xx) refusal from a permanent (5xx) one.

Directories

Path Synopsis
Package smtptest is an in-process SMTP server that captures what it is sent, so a test drives a real sender (helpers/smtp, or anything speaking SMTP) end to end and reads the delivered message back.
Package smtptest is an in-process SMTP server that captures what it is sent, so a test drives a real sender (helpers/smtp, or anything speaking SMTP) end to end and reads the delivered message back.

Jump to

Keyboard shortcuts

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