smtp

package
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

README

SMTP type

This type manages SMTP configs, defining the connection to a SMTP server used for sending email.

Exported API

The package exposes a Config struct created through NewConfig():

config, err := smtp.NewConfig()

NewConfig() reads the environment variables listed below, validates them and returns a *Config (or an error describing the first missing or invalid variable).

Config fields are private and immutable after construction; values are read through getters:

Method Returns Description
From() string Sender email address, from SMTP_FROM
Host() string SMTP server hostname, from SMTP_HOST
Port() int SMTP server port, from SMTP_PORT
Address() string SMTP server address in host:port form, built from SMTP_HOST and SMTP_PORT
Username() string SMTP authentication username
Password() string SMTP authentication password
ValidateTLS() bool Whether TLS certificates are validated

Required variables

The following env variables must be defined when using this type, none of them has a default value:

  • SMTP_FROM defines the sender email address, it must be a valid email address
  • SMTP_HOST defines SMTP server hostname, it must be a string
  • SMTP_PORT defines SMTP server port, it must be a valid port number between 1 and 65535
  • SMTP_USERNAME defines SMTP authentication username
  • SMTP_PASSWORD defines SMTP authentication password

Optional variables

  • SMTP_VALIDATE_TLS defines whether TLS certificates are validated, its default value is "true". Any value other than "true" disables validation.

Logging

Config implements slog.LogValuer, so it can be passed directly to any log/slog logger. The password field is always masked.

config, err := smtp.NewConfig()
if err != nil {
    log.Fatal(err)
}

slog.Info("SMTP config loaded", "smtp", config)
// Output: smtp.from=notifications@example.com smtp.address=smtp.example.com:587 smtp.user=mailer smtp.password=***** smtp.validate_tls=true

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

Config is a type that defines required data for connecting to a SMTP server

func NewConfig

func NewConfig() (*Config, error)

NewConfig is the function that validates and returns Config instance

func (*Config) Address added in v0.0.13

func (config *Config) Address() string

Address returns the SMTP server address in "host:port" form

func (*Config) From added in v0.0.13

func (config *Config) From() string

From returns the sender email address

func (*Config) Host added in v0.0.14

func (config *Config) Host() string

Host returns the SMTP server hostname

func (Config) LogValue

func (config Config) LogValue() slog.Value

LogValue allows to log Config masking password value

func (*Config) Password added in v0.0.13

func (config *Config) Password() string

Password returns the SMTP authentication password

func (*Config) Port added in v0.0.14

func (config *Config) Port() int

Port returns the SMTP server port

func (*Config) Username added in v0.0.13

func (config *Config) Username() string

Username returns the SMTP authentication username

func (*Config) ValidateTLS added in v0.0.13

func (config *Config) ValidateTLS() bool

ValidateTLS returns whether TLS certificates are validated

Jump to

Keyboard shortcuts

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