sendgrid

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package sendgrid sends email through SendGrid.

Choosing it commits a deployment to a SendGrid account and one API token. The caller's *http.Client is what actually issues the request — the SDK's own client supplies the request template and nothing else — so timeouts and transport instrumentation stay the caller's.

Success is a status code, and failure is not always distinguishable

SendGrid answers a send with 202 Accepted. This package treats any other status as a failure and returns ErrSendgridAPIResponse carrying the code, because the SDK reports transport errors and rejections through the same nil-error path.

The limit of that is worth knowing before choosing this provider: when an account is suspended or rate-limited to the point of not sending, the response carries no distinguishing feature. A send that will never be delivered can answer exactly like one that will, and no error is available to raise. If knowing whether mail actually left matters, it has to come from SendGrid's own event webhooks rather than from this call.

What one send is

SendEmail delivers one HTML message to one recipient, passing the caller's context through to the request. It is synchronous and does not retry; a failed send — transport error or unexpected status — scores the circuit breaker, and while that breaker is open every call returns circuitbreaking.ErrCircuitBroken.

Addressing goes through the SDK's own mail helper rather than email.FormatAddress, because the payload carries the display name and the address as separate JSON fields, with nothing for a comma to split.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrNilConfig indicates a nil config was provided.
	ErrNilConfig = platformerrors.New("SendGrid config is nil")
	// ErrEmptyAPIToken indicates an empty API token was provided.
	ErrEmptyAPIToken = platformerrors.New("empty Sendgrid API token")
	// ErrNilHTTPClient indicates a nil HTTP client was provided.
	ErrNilHTTPClient = platformerrors.New("nil sendgrid HTTP client")
)
View Source
var ErrSendgridAPIResponse = platformerrors.New("sendgrid request error")

ErrSendgridAPIResponse indicates an error occurred in SendGrid.

Functions

This section is empty.

Types

type Config

type Config struct {
	APIToken string `env:"API_TOKEN" json:"apiToken,omitempty" yaml:"apiToken,omitempty"`
}

Config configures SendGrid to send email.

func (*Config) ValidateWithContext

func (cfg *Config) ValidateWithContext(ctx context.Context) error

ValidateWithContext validates a Config struct.

type Emailer

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

Emailer uses SendGrid to send email.

func NewSendGridEmailer

func NewSendGridEmailer(cfg *Config, client *http.Client, circuitBreaker circuitbreaking.CircuitBreaker, opts ...Option) (*Emailer, error)

NewSendGridEmailer returns a new SendGrid-backed Emailer.

func (*Emailer) SendEmail

func (e *Emailer) SendEmail(ctx context.Context, details *email.OutboundEmailMessage) error

SendEmail sends an email.

type Option

type Option func(*options)

Option configures the Emailer this package constructs. The zero configuration works: an absent logger logs nowhere, an absent tracer provider traces nowhere, and an absent metrics provider records nothing.

func WithLogger

func WithLogger(logger logging.Logger) Option

WithLogger attaches a logger.

func WithMetricsProvider

func WithMetricsProvider(metricsProvider metrics.Provider) Option

WithMetricsProvider attaches a metrics provider.

func WithTracerProvider

func WithTracerProvider(tracerProvider tracing.Provider) Option

WithTracerProvider attaches a tracer provider, enabling spans on every send.

Jump to

Keyboard shortcuts

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