send

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: MIT Imports: 17 Imported by: 1

Documentation

Overview

Package send exposes primitives to send emails by responding to CloudEvents.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmailEvent

func EmailEvent(app *App) func(context.Context, cloudevents.Event) error

EmailEvent creates a function to send an email by responding to a CloudEvent.

Types

type App

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

App defines the dependencies the application uses.

func NewApp

func NewApp(opts ...AppOption) *App

NewApp is a constructor for App which utilizes the options pattern.

type AppOption

type AppOption func(*App)

func AppWithDomainSender

func AppWithDomainSender(domain string, sender Sender) AppOption

AppWithDomainSender associates a domain with a Sender. Domains will be matched with event supplied [EventData.Sender] i.e. Sender = no-reply@tommymay.dev: domain = tommymay.dev. The matching sender will be used to send the email.

func AppWithErrorLogger

func AppWithErrorLogger(logger *log.Logger) AppOption

AppWithErrorLogger provides an option to supply an error severity logger.

func AppWithFileStorage

func AppWithFileStorage(fileStorage *blob.Bucket) AppOption

AppWithFileStorage provides an option to specify where email templates should be read from. This can be a local file system, GCS, S3 or any of the other storages supported by the blob package.

func AppWithFlusher

func AppWithFlusher(flusher Flusher) AppOption

func AppWithInfoLogger

func AppWithInfoLogger(logger *log.Logger) AppOption

AppWithInfoLogger provides an option to supply an info severity logger.

func AppWithLogger

func AppWithLogger(logger *log.Logger) AppOption

AppWithLogger is a shortcut for AppWithInfoLogger and AppWithErrorLogger when the logger is the same between the two.

type EventData

type EventData struct {
	// Sender is who the email is from.
	Sender string `json:"sender"`
	// Subject is the email subject line.
	Subject string `json:"subject"`
	// Body is the email body and can be HTML. It will be parsed as a [Go HTML template] and bound to the variables
	// provided by Data. You should not pass both [EventData.Body] and [EventData.Template] at the same
	// time as they are both meant to represent the email body.
	//
	// [Go HTML template]: https://pkg.go.dev/html/template
	Body string `json:"body"`
	// To represents who the email should go to and can be provided as an array of strings a string.
	To MessageTo `json:"to"`
	// Cc represents who will be carbon copied onto the email and can be provided as an array of string or a string.
	Cc MessageTo `json:"cc"`
	// Bcc represents who will be blind carbon copied onto the email and can be provided as an array of string or a
	// string.
	Bcc MessageTo `json:"bcc"`
	// Template is a path to the email template to use as the email [EventData.Body]. It will be parsed as
	// a [Go HTML template] and bound to the variables provided by Data. You should not pass both
	// [EventData.Template] and [EventData.Body] at the same time as they are both meant to represent
	// the email body.
	//
	// [Go HTML template]: https://pkg.go.dev/html/template
	Template string `json:"template"`
	// Data is an arbitrary map of variables to values that will be used in the [EventData.Template] or
	// [EventData.Body] using [Go HTML templates].
	//
	// [Go HTML templates]: https://pkg.go.dev/html/template
	Data map[string]interface{} `json:"data"`
}

EventData is this email packages specific event payload data needed to actually send an email.

type Flusher

type Flusher interface {
	Flush() error
}

Flusher is used to flush any buffers that may need cleared before exiting the EmailEvent function. This is useful in the context of lambdas like GCP Cloud Functions where it is not reliable that there will be CPU or memory allocated after the EmailEvent function ends.

type MailgunSenderAdapter

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

MailgunSenderAdapter allows a mailgun.Mailgun interface to become compatible with the Sender interface.

func (MailgunSenderAdapter) Send

func (adapter MailgunSenderAdapter) Send(ctx context.Context, m Message) (string, error)

type Message

type Message struct {
	Sender  string
	Subject string
	Body    string
	To      []string
	Cc      []string
	Bcc     []string
}

Message represents an email.

type MessageTo

type MessageTo []string

MessageTo represents who an email should be sent to.

func (*MessageTo) UnmarshalJSON

func (to *MessageTo) UnmarshalJSON(data []byte) error

type NoopSender

type NoopSender struct{}

NoopSender implements the Sender interface but doesn't actually send any emails which is helpful for testing purposes.

func (NoopSender) Send

func (ns NoopSender) Send(ctx context.Context, m Message) (string, error)

type PubSubMessage

type PubSubMessage struct {
	Attributes  map[string]string `json:"attributes"`
	MessageId   string            `json:"messageId"`
	PublishTime string            `json:"publishTime"`
	// Data is automatically decoded from base64.
	Data []byte `json:"data"`
}

PubSubMessage is the PubsubMessage format when the message comes from Google's Pub/Sub.

type PubSubPayload

type PubSubPayload struct {
	// Subscription name that this event is associated with.
	Subscription string        `json:"subscription"`
	Message      PubSubMessage `json:"message"`
}

PubSubPayload represents GCP pub/sub MessagePublishedData format.

type ReadTemplateError

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

ReadTemplateError represents an error that occurs when an email template fails to be retrieved/read.

func (ReadTemplateError) Error

func (readTemplateError ReadTemplateError) Error() string

type Sender

type Sender interface {
	Send(ctx context.Context, m Message) (string, error)
}

Sender sends an email with the provided Message and returns the ID identifying the request. It should be noted that not all email providers provide any such ID, and therefore it may be an empty string.

func NewMailgunSender

func NewMailgunSender(mailgun mailgun.Mailgun) Sender

NewMailgunSender constructs a MailgunSenderAdapter

Jump to

Keyboard shortcuts

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