Skip to content
Gasv0.10.0

gas/email

email/ses

import "github.com/gasmod/gas/email/ses"

Package ses is an email sender on Amazon SES, as a gas.EmailProvider. It sends with the SendEmail operation of the SES API v2 and the AWS SDK for Go v2, so it also reaches an emulator that speaks the SES API v2, such as aws-ses-v2-local.

Service implements gas.Service, gas.EmailProvider and gas.ReadyReporter, and keeps the contract of the email package.

In an app, the container builds the sender from New and calls its Init. By hand, with a configuration given in code:

cfg := ses.DefaultConfig()
cfg.Email.Region = "eu-west-1"
cfg.Email.FromEmail = "Shop <shop@example.com>"
mailer, err := ses.New(ses.WithConfig(cfg))(templates, nil, logger)
if err != nil {
return err
}
if err := mailer.Init(ctx); err != nil {
return err
}
defer mailer.Close(ctx)
err = mailer.Send(ctx, &gas.Email{
To: []string{"alice@example.com"},
Subject: "Your order",
TextBody: "It ships today.",
})

After this SES has accepted the message for delivery, from Shop <shop@example.com>, an identity SES must have verified.

The settings live in the email section, in Settings:

  • email.region: the AWS region. Default: none; the AWS SDK’s default chain decides, as under Credentials.
  • email.endpoint: the URL requests go to, an absolute http or https URL with a host, and with no user information, query, fragment, or path but /, such as an emulator’s. Default: none, the region’s SES endpoint. An http endpoint sends every request in clear text, the access key ID, the session token and the messages included. The secret key signs the requests and is not sent. Use it only for an emulator on a network of its own.
  • email.fromemail: the sender of a message whose From is empty, one email address as the email package reads one, which Init normalizes. Default: none, so every message needs a From.
  • email.accesskeyid, email.secretaccesskey, email.sessiontoken: static credentials, as under Credentials. The access key ID is not a secret, but it maps to the AWS account ID, and the sender never logs it. The secret key and the token are each a Secret, which prints, marshals to JSON or YAML, and logs with log/slog as [redacted], and on which encoding/gob fails rather than encode the value. Default: none.
  • email.optimeout: the bound of each send, retries included, above 0, as under Timeouts. Default: DefaultOpTimeout (10s).
  • email.maxretries: how many times a failed send is retried, 0 to MaxRetries (10), with the AWS SDK’s standard retryer and its backoff, whatever AWS_MAX_ATTEMPTS and AWS_RETRY_MODE say. Default: DefaultMaxRetries, 2, so 3 attempts.
  • email.rawerrors: whether a failed send’s error also shows and wraps the AWS SDK’s error, as under Error replies. It breaks the guarantees under Security, so use it to debug. Default: false.

Service.Init resolves the configuration each time it runs, from one of two sources:

  • Without WithConfig, it binds DefaultConfig from the gas.ConfigProvider, which checks the validate tags. A nil ConfigProvider is the error email.ErrNoConfigProvider.
  • With WithConfig, it takes a copy of the given Config, never changing the caller’s struct, and checks the same tags.

Either way, these rules then run, in this order, and the first that fails is the error:

  1. endpoint, when set, is an absolute http or https URL with a host, and with no user information, query, fragment, or path but /.
  2. fromemail, when set, is one email address.

A failure wraps email.ErrInvalidConfig and names the setting, never its value.

Service.Config returns a copy of the configuration the last successful Init resolved, defaults included, or the zero Config before one. A failed Init and Close leave it as it was. Its fromemail is the normalized address, and its secretaccesskey and sessiontoken stay a Secret, which prints as [redacted].

Init builds the client with the AWS SDK’s default configuration, which comes from:

  • the environment, such as AWS_REGION and AWS_ACCESS_KEY_ID;
  • the shared configuration and credentials files, and their profile;
  • the container and instance roles.

Its HTTP client is the sender’s own, on the AWS SDK’s default transport. It follows no redirect, as the SDK’s does not.

A region setting takes precedence over the chain’s region. Without one, a region the chain does not find fails Init with ErrNoRegion.

Static credentials are used when both accesskeyid and secretaccesskey are set, with sessiontoken when it is set too. With either key empty, the chain’s credentials apply, as config/secretsmanager and gas/queue/sqs do it, and the other static values are not used. When some of them are set, but not both keys, Init logs static credentials incomplete, using the default chain at warn level, with missing naming the keys that are not set, never a value.

AWS configuration that fails to load, such as a profile that does not exist, fails Init with email.ErrInvalidConfig.

Init retrieves the credentials within its context and optimeout, so that it fails, with ErrNoCredentials, when there are none, rather than the first send. The client then refreshes them as they expire. Init sends no request to SES. The credentials need the ses:SendEmail permission.

WithClient gives the sender a client to use instead, such as an *sesv2.Client built elsewhere or a fake of API. region, endpoint, the credentials and maxretries then have no effect, and Init neither checks a region nor retrieves credentials.

Send is one SendEmail request with Simple content, which SES assembles into a MIME message:

  • FromEmailAddress is the normalized sender.
  • ReplyToAddresses is the reply-to address, when there is one.
  • Destination holds the To, Cc and Bcc lists, each left out when it is empty.
  • The subject and each body that is not empty are sent with the UTF-8 character set. An empty body is left out.
  • A subject that is not ASCII is sent as RFC 2047 encoded words, base64 of its UTF-8, which the sender encodes itself, so that SES sends it as it is.
  • The headers are sent in the sorted order of their names.

No configuration set, tag, list management option or attachment is sent. The message ID SES returns is not kept: the interface returns only an error.

Every send runs within its context and optimeout, whichever ends first, the AWS SDK’s retries and their backoff included. So a server that stops answering cannot hold a send longer than optimeout, even with a context that never ends. A SendFromTemplate fetches its templates within its context alone, then sends as Send does.

The SDK retries a send after a network error, a throttle, such as TooManyRequestsException or LimitExceededException, and a server error, up to maxretries times.

SES has no idempotency key, so a send whose reply was lost and that is retried can deliver the message twice. Set maxretries to 0 where a duplicate is worse than a lost message, and retry in the app with what it knows.

A send that fails at the connection returns an error wrapping ErrConnect and a reason, never the AWS SDK’s error, which holds the endpoint, unless email.rawerrors is set, as under Error replies. The reasons:

  • timed out: a deadline passed, in a context or on the network. The error then wraps context.DeadlineExceeded too.
  • connection refused: the endpoint refused the TCP connection.
  • unreachable: any other network error, such as a failed name lookup.

A send that got an error reply of SES, as under Error replies, and whose retries a deadline then cut short is told by the reply’s code, not as timed out, and wraps context.DeadlineExceeded too. A throttled send that optimeout ends is throttled.

A send whose own context ended returns the context’s error after the operation, wrapped, without ErrConnect.

Which of the two a timeout is depends on which deadline was in force. A timeout of a send whose context’s deadline is no later than optimeout’s is the context’s error.

An error reply of SES returns an error wrapping ErrCommand, with a reason told by its AWS error code. The code follows, in parentheses, when it is one SES or the AWS APIs document. The reply’s message, which can quote an address, is never kept, nor a code of any other text, nor the AWS SDK’s error.

With email.rawerrors set, the error’s text ends with : and the AWS SDK’s error, its operation, HTTP status, request ID, code and message, and the error wraps it, so errors.As reaches the SDK’s types, such as smithy.APIError. It does the same for a connection error, and for the failure of Init to load the AWS configuration or to retrieve credentials. The reason, the sentinels and the context’s errors are as without it. Init logs a warning when it is set.

The reasons, in the order they are told:

  • not verified: MessageRejected whose message says an address is not verified, such as the sender, or a recipient of an account in the SES sandbox, also wrapping ErrNotVerified and ErrRejected; and MailFromDomainNotVerifiedException, also wrapping ErrNotVerified.
  • rejected: any other MessageRejected, also wrapping ErrRejected.
  • throttled: TooManyRequestsException, LimitExceededException and the other throttles, past the retries, also wrapping ErrThrottled.
  • sending paused: SendingPausedException and AccountSuspendedException, also wrapping ErrSendingPaused.
  • access denied: AccessDenied, InvalidClientTokenId, SignatureDoesNotMatch, ExpiredToken and the other codes of refused credentials or permissions, also wrapping ErrAccessDenied.
  • invalid reply: a reply SES does not send: a body larger than 1 MiB, which the client Init builds stops reading at, or a client of WithClient that returns neither an output nor an error.
  • failed: any other reply, such as BadRequestException, or a send that failed for another reason, such as a reply that is not valid JSON.

A successful Init publishes the client and logs service initialized at info level, with:

  • region;
  • whether an endpoint, a client and a default sender, from, were given;
  • credentials, where the client Init built takes them from, static or default chain, left out with WithClient;
  • op_timeout and max_retries.

Only then do the methods use the client. Init on an initialized sender returns email.ErrAlreadyInitialized and changes nothing. A failed Init leaves the sender as it was.

New’s constructor fails with:

  • email.ErrNilTemplates for a nil gas.TemplateProvider;
  • email.ErrNilLogger for a nil logger;
  • email.ErrNilConfig for WithConfig(nil).

A nil ConfigProvider is accepted, and Init then requires WithConfig. A nil client given to WithClient fails Init with ErrNilClient.

Service.Close runs these steps, in order:

  1. It makes the sender closed, so that every later call returns email.ErrClosed.
  2. It waits for every send in flight to return. While its context lasts, it does not cut a send short.
  3. It closes the idle connections of the client Init built.
  4. It logs service closed at info level.

When its context ends first, Close:

  1. cancels the sends in flight, which return email.ErrClosed, though SES may have accepted their messages;
  2. logs service closed, sends in flight canceled at warn level;
  3. returns an error wrapping the context’s error.

The sender stays closed. Close on a sender that is not initialized returns nil and logs nothing. Init after Close builds a new client, with a configuration resolved afresh.

Service.CheckReady does no I/O: it fails only when the sender is not initialized or its context has ended. It does not ask SES whether the account may send.

Service.Client returns the *sesv2.Client, for the operations the sender has no method for, such as GetAccount. It returns nil when the sender is not initialized or was given another API.

Requests sent on it are bounded only by their own context, and return the AWS SDK’s errors as they are, which can hold the endpoint and addresses.

No error the sender makes and no log it writes holds an address, a subject, a body, a header value, template data, the endpoint, or a credential, the access key ID included, unless email.rawerrors is set: then an error can hold what the AWS SDK’s error holds, such as the endpoint, addresses and the server’s message. The logs carry:

  • the region;
  • whether an endpoint, a client and a default sender were given;
  • where the credentials come from;
  • the names of the static keys missing;
  • the operation timeout and the retries.

fmt, encoding/json, YAML encoders that use encoding.TextMarshaler, and log/slog write the secret key and the session token of a Settings or a Config as [redacted]. encoding/gob fails on them rather than encode them.

Static credentials in configuration are a secret of the app’s. Prefer a role, which the default chain finds.

An http endpoint sends the access key ID, the session token and the messages in clear text, as under Configuration.

The client Init builds reads no reply past 1 MiB, so an endpoint cannot make it read without bound. It logs nothing: the AWS SDK’s default logger, which writes raw transport errors holding the endpoint, is replaced with one that discards them.

A Service is safe for concurrent use. Init and Close run one at a time.

Init swaps in what it built at once. So a send, which takes no lock but to count itself in flight, uses one Init’s client throughout.

The sentinel errors of this package, to be tested with errors.Is:

The other errors wrap a sentinel of the email package, as the email package describes. Every error starts with ServiceName and the operation. A configuration that fails its tags gets the validator’s error after gas/email/ses: init: invalid config: validate config: , such as:

Key: 'Config.Email.OpTimeout' Error:Field validation for 'OpTimeout' failed on the 'gt' tag

The other errors, as returned, with send standing for both send operations where they fail the same way:

gas/email/ses: template provider is nil
gas/email/ses: logger is nil
gas/email/ses: config is nil
gas/email/ses: init: no config provider
gas/email/ses: init: invalid config: Email.Endpoint must be an http or https URL with a host and nothing more but /
gas/email/ses: init: invalid config: Email.FromEmail must be one email address
gas/email/ses: init: invalid config: AWS configuration failed to load
gas/email/ses: init: no region
gas/email/ses: init: no credentials: failed
gas/email/ses: init: no credentials: timed out
gas/email/ses: init: no credentials: timed out: context deadline exceeded
gas/email/ses: init: client is nil
gas/email/ses: init: service already initialized
gas/email/ses: init: context canceled
gas/email/ses: ready: service not initialized
gas/email/ses: send: message is nil
gas/email/ses: send: service not initialized
gas/email/ses: send: service closed
gas/email/ses: send: context canceled
gas/email/ses: send: context deadline exceeded
gas/email/ses: send: invalid address: To[0]: not one email address
gas/email/ses: send: timed out
gas/email/ses: send: connection refused
gas/email/ses: send: unreachable
gas/email/ses: send: not verified (MessageRejected)
gas/email/ses: send: not verified (MailFromDomainNotVerifiedException)
gas/email/ses: send: rejected (MessageRejected)
gas/email/ses: send: throttled (TooManyRequestsException)
gas/email/ses: send: sending paused (SendingPausedException)
gas/email/ses: send: access denied (AccessDeniedException)
gas/email/ses: send: failed (BadRequestException)
gas/email/ses: send: failed
gas/email/ses: send: invalid reply
gas/email/ses: send from template: template failed: subject "welcome-subject.txt": fetch failed
gas/email/ses: close: context deadline exceeded

const (
// DefaultOpTimeout is the bound of each send of DefaultConfig, its
// retries included.
DefaultOpTimeout = 10 * time.Second
// DefaultMaxRetries is how many times DefaultConfig retries a failed
// send: 2, so 3 attempts, as the AWS SDK's standard retryer makes by
// default.
DefaultMaxRetries = 2
// MaxRetries is the most retries the MaxRetries setting allows.
MaxRetries = 10
)

const ServiceName = "gas/email/ses"

ServiceName is the name of the SES sender, returned by Service.Name and prefixed to every error the sender returns.

var (
// ErrNoRegion indicates that Init found no AWS region, in the Region
// setting or the AWS SDK's default chain.
ErrNoRegion = errors.New("no region")
// ErrNoCredentials indicates that Init could not retrieve AWS
// credentials.
ErrNoCredentials = errors.New("no credentials")
// ErrNilClient indicates that WithClient was given a nil client.
ErrNilClient = errors.New("client is nil")
// ErrConnect indicates that a send got no reply from SES: it timed
// out, its connection was refused, or the endpoint was unreachable, as
// described in the package documentation.
ErrConnect = errors.New("no reply")
// ErrCommand indicates that SES replied to a send with an error, or
// that a send failed for another reason than ErrConnect's, as
// described in the package documentation.
ErrCommand = errors.New("request failed")
// ErrRejected indicates that SES refused the message, with
// MessageRejected. It comes with ErrCommand.
ErrRejected = errors.New("rejected")
// ErrNotVerified indicates that SES has not verified the sender's
// address or domain, or, in the sandbox, a recipient. It comes with
// ErrCommand.
ErrNotVerified = errors.New("not verified")
// ErrThrottled indicates that SES throttled the send, past the
// retries. It comes with ErrCommand.
ErrThrottled = errors.New("throttled")
// ErrSendingPaused indicates that the account may not send: its
// sending is paused or suspended. It comes with ErrCommand.
ErrSendingPaused = errors.New("sending paused")
// ErrAccessDenied indicates that SES refused the credentials, or their
// permission for the send. It comes with ErrCommand.
ErrAccessDenied = errors.New("access denied")
)

func New(
opts ...Option,
) func(gas.TemplateProvider, gas.ConfigProvider, gas.Logger) (*Service, error)

New returns the constructor of a Service with opts applied, in the shape the gas container expects. The constructor returns an error wrapping email.ErrNilTemplates for a nil TemplateProvider, email.ErrNilLogger for a nil logger, and email.ErrNilConfig for WithConfig(nil); a nil ConfigProvider is accepted, and Init then requires WithConfig.

type API interface {
// SendEmail sends a message.
SendEmail(
ctx context.Context,
params *sesv2.SendEmailInput,
optFns ...func(*sesv2.Options),
) (*sesv2.SendEmailOutput, error)
}

API is the part of the SES API v2 client the sender uses. *sesv2.Client implements it.

type Config struct {
// Email holds the settings.
Email Settings
}

Config holds the SES sender’s configuration, read from the email section.

func DefaultConfig() *Config

DefaultConfig returns the configuration a sender starts from before binding: no region, no endpoint, no default sender, no static credentials, DefaultOpTimeout and DefaultMaxRetries.

type Option func(*Service)

Option configures a Service. Options only record what they are given; the constructor and Init check it.

func WithClient(client API) Option

WithClient makes the sender send with client, such as an *sesv2.Client built elsewhere or a fake, instead of building one, as described in the package documentation. The Region, Endpoint, credential and MaxRetries settings then have no effect. A nil client, or a nil *sesv2.Client, makes Init fail with ErrNilClient.

Default: Init builds an *sesv2.Client from the settings.

func WithConfig(cfg *Config) Option

WithConfig makes Init use a copy of cfg instead of binding the configuration from the ConfigProvider. Init copies *cfg each time it runs and never changes it; the copy is checked against its validate tags as a bound configuration is. A nil cfg makes the constructor fail with email.ErrNilConfig.

Default: bind the email section from the ConfigProvider over DefaultConfig.

type Secret = gas.Secret

Secret is gas.Secret: a string that is never printed, marshaled or logged, such as Settings.SecretAccessKey. Configuration binds into it as into a string, and string(s) is the value.

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

Service is an email sender on Amazon SES, as described in the package documentation. It is safe for concurrent use.

func (s *Service) CheckReady(ctx context.Context) error

CheckReady reports readiness without I/O: an error wrapping email.ErrNotInitialized or email.ErrClosed when the sender is not initialized, the context’s error when ctx has ended, and else nil.

func (s *Service) Client() *sesv2.Client

Client returns the *sesv2.Client the sender sends with, or nil when the sender is not initialized or was given another API with WithClient, as described in the package documentation.

func (s *Service) Close(ctx context.Context) error

Close makes every later call but Init return an error wrapping email.ErrClosed, and waits for every send in flight to return, as described in the package documentation. When ctx ends first, it cancels the sends in flight, which return email.ErrClosed, and returns an error wrapping ctx’s error; the sender stays closed. Close then closes the idle connections of a client Init built. Close on a sender that is not initialized returns nil and logs nothing.

func (s *Service) Config() Config

Config returns a copy of the configuration the last successful Init resolved, defaults included, or the zero Config before one. A failed Init and Close leave it as it was. Config holds no slice, map or pointer, so the copy shares nothing with the sender, and SecretAccessKey and SessionToken stay Secrets.

func (s *Service) Init(ctx context.Context) error

Init resolves and checks the configuration, builds the client and retrieves the credentials, as described in the package documentation. On an initialized sender it returns email.ErrAlreadyInitialized and changes nothing. A failed Init leaves the sender as it was.

func (*Service) Name() string

Name returns ServiceName.

func (s *Service) Send(ctx context.Context, msg *gas.Email) error

Send checks msg as email.CheckEmail does and sends a normalized copy of it with SendEmail, as described in the package documentation. It never changes msg.

func (s *Service) SendFromTemplate(ctx context.Context, msg *gas.TemplatedEmail) error

SendFromTemplate renders msg into a copy, as the email package describes under Templates, and sends the copy as Send does. It never changes msg.

type Settings struct {
// Region is the AWS region; empty leaves it to the AWS SDK's default
// chain.
Region string
// Endpoint is the URL requests go to, such as an emulator's; empty is
// the region's SES endpoint.
Endpoint string
// FromEmail is the sender of a message whose From is empty; empty is
// none.
FromEmail string
// AccessKeyID is the access key ID of static credentials, used only
// with SecretAccessKey. It is not a secret, but an identifier that
// maps to the AWS account ID, so the sender never logs it.
AccessKeyID string
// SecretAccessKey is the secret access key of static credentials,
// used only with AccessKeyID. It prints, marshals and logs as
// `[redacted]`.
SecretAccessKey Secret
// SessionToken is the session token of temporary static credentials.
// It prints, marshals and logs as `[redacted]`.
SessionToken Secret
// OpTimeout bounds each send, its retries included.
OpTimeout time.Duration `validate:"gt=0"`
// MaxRetries is how many times a failed send is retried.
MaxRetries int `validate:"min=0,max=10"`
// RawErrors makes a failed send's error also show and wrap the AWS
// SDK's error, which can hold the endpoint, addresses and the
// server's message. Off by default; use it to debug.
RawErrors bool
}

Settings are the SES sender’s settings, as described in the package documentation.