Documentation ¶
Overview ¶
Example ¶
Configure postdog from YAML file.
package main import ( "bytes" "context" "github.com/bounoable/postdog/autowire" "github.com/bounoable/postdog/letter" ) func main() { // Load YAML config cfg, err := autowire.File("/path/to/config.yml") if err != nil { panic(err) } // Build office off, err := cfg.Office(context.Background()) if err != nil { panic(err) } // Send mail with default transport err = off.Send( context.Background(), letter.Write( letter.From("Bob", "bob@belcher.test"), letter.To("Calvin", "calvin@fishoeder.test"), letter.To("Felix", "felix@fishoeder.test"), letter.BCC("Jimmy", "jimmy@pesto.test"), letter.Subject("Hi, buddy."), letter.Text("Have a drink later?"), letter.HTML("Have a <strong>drink</strong> later?"), letter.MustAttach(bytes.NewReader([]byte("secret")), "my_burger_recipe.txt"), ), ) // or use a specific transport err = off.SendWith( context.Background(), "mytransport", letter.Write( letter.From("Bob", "bob@belcher.test"), // ... ), ) }
Output:
Example (ManualConfiguration) ¶
package main import ( "context" "github.com/bounoable/postdog" "github.com/bounoable/postdog/letter" "github.com/bounoable/postdog/transport/gmail" "github.com/bounoable/postdog/transport/smtp" ) func main() { po := postdog.New() smtpTransport := smtp.NewTransport("smtp.mailtrap.io", 587, "abcdef123456", "123456abcdef") gmailTransport, err := gmail.NewTransport( context.Background(), gmail.CredentialsFile("/path/to/service_account.json"), ) if err != nil { panic(err) } po.ConfigureTransport("mytransport1", smtpTransport) po.ConfigureTransport("mytransport2", gmailTransport, postdog.DefaultTransport()) // Make "mytransport2" the default err = po.Send( context.Background(), letter.Write( letter.From("Bob", "bob@belcher.test"), // ... ), ) }
Output:
Example (Middleware) ¶
package main import ( "context" "strings" "github.com/bounoable/postdog" "github.com/bounoable/postdog/letter" ) func main() { off := postdog.New( postdog.WithMiddleware( postdog.MiddlewareFunc(func( ctx context.Context, let letter.Letter, next func(context.Context, letter.Letter) (letter.Letter, error), ) (letter.Letter, error) { let.Subject = strings.Title(let.Subject) return next(ctx, let) }), ), ) if err := off.Send(context.Background(), letter.Write( letter.Subject("this is a title"), // will be set to "This Is A Title" by the middleware before sending )); err != nil { panic(err) } }
Output:
Example (UseTransportDirectly) ¶
package main import ( "context" "github.com/bounoable/postdog/letter" "github.com/bounoable/postdog/transport/gmail" ) func main() { trans, err := gmail.NewTransport( context.Background(), gmail.CredentialsFile("/path/to/service_account.json"), ) if err != nil { panic(err) } let := letter.Write( letter.From("Bob", "bob@belcher.test"), // ... ) if err := trans.Send(context.Background(), let); err != nil { panic(err) } }
Output:
Index ¶
- Constants
- Variables
- func SendError(ctx context.Context) error
- type Config
- type ConfigureOption
- type DispatchOption
- type Logger
- type Middleware
- type MiddlewareFunc
- type Office
- func (o *Office) Config() Config
- func (o *Office) ConfigureTransport(name string, trans Transport, options ...ConfigureOption)
- func (o *Office) DefaultTransport() (Transport, error)
- func (o *Office) Dispatch(ctx context.Context, let letter.Letter, opts ...DispatchOption) error
- func (o *Office) MakeDefault(name string) error
- func (o *Office) Run(ctx context.Context, opts ...RunOption) error
- func (o *Office) Send(ctx context.Context, let letter.Letter) error
- func (o *Office) SendWith(ctx context.Context, transport string, let letter.Letter) error
- func (o *Office) Transport(name string) (Transport, error)
- type Option
- type Plugin
- type PluginContext
- type PluginFunc
- type RunOption
- type SendHook
- type Transport
- type UnconfiguredTransportError
Examples ¶
Constants ¶
const ( // BeforeSend hook is called before a letter is sent. BeforeSend = SendHook(iota) // AfterSend hook is called after a letter has been sent. AfterSend )
Variables ¶
var ( // DefaultLogger is the default logger implementation. // It redirects all Log(v ...interface{}) calls to log.Println(v...) DefaultLogger defaultLogger // NopLogger is a nil logger and effectively a no-op logger. NopLogger Logger )
Functions ¶
Types ¶
type Config ¶
type Config struct { // Logger is an optional Logger implementation. Logger Logger // QueueBuffer is the channel buffer size for outgoing letters. QueueBuffer int // Middleware is the send middleware. Middleware []Middleware // SendHooks contains the callback functions for the send hooks. SendHooks map[SendHook][]func(context.Context, letter.Letter) // Plugins are the plugins to used. Plugins []Plugin }
Config is the office configuration.
type ConfigureOption ¶
type ConfigureOption func(*configureConfig)
ConfigureOption is a transport configuration option.
func DefaultTransport ¶
func DefaultTransport() ConfigureOption
DefaultTransport makes a transport the default transport.
type DispatchOption ¶
type DispatchOption func(*dispatchJob)
DispatchOption is a Dispatch() option.
func DispatchWith ¶
func DispatchWith(transport string) DispatchOption
DispatchWith sets the transport to be used for sending the letter.
type Middleware ¶
type Middleware interface { Handle( ctx context.Context, let letter.Letter, next func(context.Context, letter.Letter) (letter.Letter, error), ) (letter.Letter, error) }
Middleware intercepts and manipulates letters before they are sent. A Middleware that returns an error, aborts the sending of the letter with the returned error.
type MiddlewareFunc ¶
type MiddlewareFunc func( ctx context.Context, let letter.Letter, next func(context.Context, letter.Letter) (letter.Letter, error), ) (letter.Letter, error)
MiddlewareFunc allows the use of functions as middlewares.
type Office ¶
type Office struct {
// contains filtered or unexported fields
}
Office queues and dispatches outgoing letters. It is thread-safe (but the transports may not be).
func NewWithConfig ¶
NewWithConfig initializes a new *Office with the given cfg.
func (*Office) ConfigureTransport ¶
func (o *Office) ConfigureTransport(name string, trans Transport, options ...ConfigureOption)
ConfigureTransport configures the transport with the given name. The first configured transport is the default transport, even if the Default() option is not used. Subsequent calls to ConfigureTransport() with the Default() option override the default transport.
func (*Office) DefaultTransport ¶
DefaultTransport returns the default transport. Returns an UnconfiguredTransportError if no transport has been configured.
func (*Office) Dispatch ¶
Dispatch adds let to the send queue with the given opts. Dispatch returns an error only if ctx is canceled before let has been queued. Available options:
DispatchWith(): Set the name of the transport to use.
func (*Office) MakeDefault ¶
MakeDefault makes the transport with the given name the default transport.
func (*Office) Run ¶
Run processes the outgoing letter queue with the given options. Run blocks until ctx is canceled. Available options:
Workers(): Set the queue worker count.
func (*Office) SendWith ¶
SendWith sends a letter over the given transport. Returns an UnconfiguredTransportError, if the transport with the given name has not been registered. If a middleware returns an error, SendWith() will return that error. The BeforeSend hook is called after the middlewares, before Transport.Send(). The AfterSend hook is called after Transport.Send(), even if Transport.Send() returns an error. Hooks are called concurrently.
type Option ¶
type Option func(*Config)
Option is an office option.
func QueueBuffer ¶
QueueBuffer sets the outgoing letter channel buffer size.
func WithLogger ¶
WithLogger configures the office to use the given logger.
func WithMiddleware ¶
func WithMiddleware(middleware ...Middleware) Option
WithMiddleware adds middleware to the office.
func WithPlugin ¶
WithPlugin installs the given office plugins.
type PluginContext ¶
type PluginContext interface { // Log logs messages. Log(...interface{}) // WithSendHook adds callback functions for send hooks. WithSendHook(SendHook, ...func(context.Context, letter.Letter)) // WithMiddleware adds send middleware. WithMiddleware(...Middleware) }
PluginContext is the context for a plugin. It provides logging and configuration functions.
type PluginFunc ¶
type PluginFunc func(PluginContext)
PluginFunc allows functions to be used as plugins.
func (PluginFunc) Install ¶
func (fn PluginFunc) Install(ctx PluginContext)
Install installs an office plugin.
type UnconfiguredTransportError ¶
type UnconfiguredTransportError struct {
Name string
}
UnconfiguredTransportError means a transport has not been registered.
func (UnconfiguredTransportError) Error ¶
func (err UnconfiguredTransportError) Error() string
Directories ¶
Path | Synopsis |
---|---|
api
|
|
Package autowire provides office initialization through a YAML config.
|
Package autowire provides office initialization through a YAML config. |
examples
|
|
Package mock_postdog is a generated GoMock package.
|
Package mock_postdog is a generated GoMock package. |
plugin
|
|
markdown
Package markdown provides Markdown support for letters.
|
Package markdown provides Markdown support for letters. |
markdown/goldmark
Package goldmark provides an adapter for the goldmark Markdown parser.
|
Package goldmark provides an adapter for the goldmark Markdown parser. |
markdown/mock_markdown
Package mock_markdown is a generated GoMock package.
|
Package mock_markdown is a generated GoMock package. |
store
Package store provides a storage plugin for sent letters.
|
Package store provides a storage plugin for sent letters. |
store/mock_store
Package mock_store is a generated GoMock package.
|
Package mock_store is a generated GoMock package. |
store/mongostore
Package mongostore provides the mongodb store implementation.
|
Package mongostore provides the mongodb store implementation. |
store/query
Package query provides functions to query letters from a store.
|
Package query provides functions to query letters from a store. |
store/query/mock_query
Package mock_query is a generated GoMock package.
|
Package mock_query is a generated GoMock package. |
store/storetest
Package storetest provides testing utilities that can be used to test store implementations.
|
Package storetest provides testing utilities that can be used to test store implementations. |
template
Package template provides template support for letter bodies.
|
Package template provides template support for letter bodies. |
template/i18n/mock_i18n
Package mock_i18n is a generated GoMock package.
|
Package mock_i18n is a generated GoMock package. |
transport
|
|
gmail
Package gmail provides the transport implementation for gmail.
|
Package gmail provides the transport implementation for gmail. |
smtp
Package smtp provides the transport implementation for SMTP.
|
Package smtp provides the transport implementation for SMTP. |