Documentation ¶
Overview ¶
Example (OpenSenderFromURL) ¶
package main import ( "context" "log" "time" "github.com/blockysource/blocky-cloud/email" ) func main() { ctx := context.Background() s, err := email.OpenSender(ctx, "log://?loglevel=debug&logbody=true") if err != nil { log.Fatal(err) } defer s.Shutdown(ctx) msg, err := email.ComposeMessage( email.From("\"John Doe\" <john@doe.org>"), email.To("\"Jane Doe\" <jane@doe.org>"), email.Date(time.Now()), email.Subject("Hello!"), email.TextBody("Hey Jane! This is John. How are you?"), email.HTMLBody("<strong>Hey Jane!</strong><p>This is John. How are you?</p>"), ) if err != nil { log.Fatal(err) } err = s.Send(ctx, msg) if err != nil { log.Fatal(err) } }
Output:
Example (OpenTemplatedSenderFromURL) ¶
package main import ( "context" "log" "github.com/blockysource/blocky-cloud/email" ) func main() { ctx := context.Background() s, err := email.OpenTemplatedSender(ctx, "log://?loglevel=debug&logbody=true") if err != nil { log.Fatal(err) } defer s.Shutdown(ctx) // Use s to send emails. msg, err := email.ComposeTemplatedMessage( email.From("\"John Doe\" <john@doe.com>"), email.To("\"Jane Doe\" <jane@doe.com>"), email.Subject("Hello, World!"), email.Template("welcome", "v1", map[string]any{"name": "John Doe"}), ) if err != nil { log.Fatal(err) } s.Send(context.Background(), msg) }
Output:
Index ¶
- Constants
- func OpenSender(logger *slog.Logger, opts SenderOptions) (*email.Sender, error)
- func OpenSenderLogFn(logFn func(ctx context.Context, msg *driver.Message)) (*email.Sender, error)
- func OpenTemplatedSender(logger *slog.Logger, opts SenderOptions) (*email.TemplatedSender, error)
- func OpenTemplatedSenderLogFn(logFn func(ctx context.Context, msg *driver.TemplatedMessage)) (*email.TemplatedSender, error)
- type SenderOptions
- type URLOpener
Examples ¶
Constants ¶
const Scheme = "log"
Scheme is the URL scheme logemail registers its URLOpener under on emails.DefaultMux.
Variables ¶
This section is empty.
Functions ¶
func OpenSender ¶
OpenSender opens email.Sender based on provider url.
Example ¶
package main import ( "context" "log" "os" "time" "golang.org/x/exp/slog" "github.com/blockysource/blocky-cloud/email" "github.com/blockysource/blocky-cloud/email/logemail" ) func main() { logger := slog.New(slog.NewJSONHandler(os.Stdout)) opts := logemail.SenderOptions{ LogLevel: slog.LevelInfo, LogBody: true, } s, err := logemail.OpenSender(logger, opts) if err != nil { log.Fatal(err) } defer s.Shutdown(context.Background()) // Use s to send emails. msg, err := email.ComposeMessage( email.From("\"John Doe\" <john@doe.org>"), email.To("\"Jane Doe\" <jane@doe.org>"), email.Date(time.Now()), email.Subject("Hello!"), email.TextBody("Hey Jane! This is John. How are you?"), email.HTMLBody("<strong>Hey Jane!</strong><p>This is John. How are you?</p>"), ) if err != nil { log.Fatal(err) } s.Send(context.Background(), msg) }
Output:
func OpenSenderLogFn ¶
OpenSenderLogFn opens log based email.Sender with custom log function.
Example ¶
package main import ( "context" "log" "time" "github.com/blockysource/blocky-cloud/email" "github.com/blockysource/blocky-cloud/email/driver" "github.com/blockysource/blocky-cloud/email/logemail" ) func main() { s, err := logemail.OpenSenderLogFn(func(ctx context.Context, msg *driver.Message) { // Log the message in custom way. log.Printf("msg: %+v", msg) }) if err != nil { log.Fatal(err) } defer s.Shutdown(context.Background()) msg, err := email.ComposeMessage( email.From("\"John Doe\" <john@doe.org>"), email.To("\"Jane Doe\" <jane@doe.org>"), email.Date(time.Now()), email.Subject("Hello!"), email.TextBody("Hey Jane! This is John. How are you?"), email.HTMLBody("<strong>Hey Jane!</strong><p>This is John. How are you?</p>"), ) if err != nil { log.Fatal(err) } s.Send(context.Background(), msg) }
Output:
func OpenTemplatedSender ¶
func OpenTemplatedSender(logger *slog.Logger, opts SenderOptions) (*email.TemplatedSender, error)
OpenTemplatedSender opens email.TemplatedSender based on provider url.
Example ¶
package main import ( "context" "log" "os" "golang.org/x/exp/slog" "github.com/blockysource/blocky-cloud/email" "github.com/blockysource/blocky-cloud/email/logemail" ) func main() { logger := slog.New(slog.NewJSONHandler(os.Stdout)) opts := logemail.SenderOptions{ LogLevel: slog.LevelInfo, LogBody: true, } s, err := logemail.OpenTemplatedSender(logger, opts) if err != nil { log.Fatal(err) } defer s.Shutdown(context.Background()) // Use s to send emails. msg, err := email.ComposeTemplatedMessage( email.From("\"John Doe\" <john@doe.com>"), email.To("\"Jane Doe\" <jane@doe.com>"), email.Subject("Hello, World!"), email.Template("welcome", "v1", map[string]any{"name": "John Doe"}), ) if err != nil { log.Fatal(err) } s.Send(context.Background(), msg) }
Output:
func OpenTemplatedSenderLogFn ¶
func OpenTemplatedSenderLogFn(logFn func(ctx context.Context, msg *driver.TemplatedMessage)) (*email.TemplatedSender, error)
OpenTemplatedSenderLogFn opens log based email.TemplatedSender with custom log function.
Example ¶
package main import ( "context" "log" "github.com/blockysource/blocky-cloud/email" "github.com/blockysource/blocky-cloud/email/driver" "github.com/blockysource/blocky-cloud/email/logemail" ) func main() { s, err := logemail.OpenTemplatedSenderLogFn(func(ctx context.Context, msg *driver.TemplatedMessage) { // Log the message in custom way. log.Printf("msg: %+v", msg) }) if err != nil { log.Fatal(err) } defer s.Shutdown(context.Background()) // Use s to send emails. msg, err := email.ComposeTemplatedMessage( email.From("\"John Doe\" <john@doe.com>"), email.To("\"Jane Doe\" <jane@doe.com>"), email.Subject("Hello, World!"), email.Template("welcome", "v1", map[string]any{"name": "John Doe"}), ) if err != nil { log.Fatal(err) } s.Send(context.Background(), msg) }
Output:
Types ¶
type SenderOptions ¶
type SenderOptions struct { // LogLevel is the level at which to log messages. LogLevel slog.Level // LogBody controls whether the body of the message is logged. LogBody bool }
SenderOptions are options for constructing a *email.Sender and *email.TemplatedSender for the logemail provider.
type URLOpener ¶
type URLOpener struct { // Logger is the logger used to log messages. // If nil, slog.Default is used. Logger *slog.Logger }
URLOpener is a logging email.SenderURLOpener and email.TemplatedSenderURLOpener. This should not be used in production. It is intended for use in tests and as an example or starting point for implementing email.SenderURLOpener and email.TemplatedSenderURLOpener.
func (*URLOpener) OpenSenderURL ¶
OpenSenderURL opens email.Sender based on provider url. Implements email.SenderURLOpener. The url is of the form "log://?level=debug&logbody=true".
Example ¶
package main import ( "context" "log" "net/url" "os" "time" "golang.org/x/exp/slog" "github.com/blockysource/blocky-cloud/email" "github.com/blockysource/blocky-cloud/email/logemail" ) func main() { // This example shows usage of the URLOpener with specific Logger implementation. logger := slog.New(slog.NewJSONHandler(os.Stdout)) o := &logemail.URLOpener{Logger: logger} u := &url.URL{Scheme: logemail.Scheme, Host: ""} s, err := o.OpenSenderURL(context.Background(), u) if err != nil { log.Fatal(err) } defer s.Shutdown(context.Background()) msg, err := email.ComposeMessage( email.From("\"John Doe\" <john@doe.org>"), email.To("\"Jane Doe\" <jane@doe.org>"), email.Date(time.Now()), email.Subject("Hello!"), email.TextBody("Hey Jane! This is John. How are you?"), email.HTMLBody("<strong>Hey Jane!</strong><p>This is John. How are you?</p>"), ) if err != nil { log.Fatal(err) } s.Send(context.Background(), msg) }
Output:
func (*URLOpener) OpenTemplatedSenderURL ¶
func (o *URLOpener) OpenTemplatedSenderURL(ctx context.Context, u *url.URL) (*email.TemplatedSender, error)
OpenTemplatedSenderURL opens email.TemplatedSender based on provider url. Implements email.TemplatedSenderURLOpener. The url is of the form "log://?level=debug&logbody=true".
Example ¶
package main import ( "context" "log" "net/url" "os" "golang.org/x/exp/slog" "github.com/blockysource/blocky-cloud/email" "github.com/blockysource/blocky-cloud/email/logemail" ) func main() { // This example shows usage of the URLOpener with specific Logger implementation. logger := slog.New(slog.NewJSONHandler(os.Stdout)) o := &logemail.URLOpener{Logger: logger} u := &url.URL{Scheme: logemail.Scheme, Host: ""} s, err := o.OpenTemplatedSenderURL(context.Background(), u) if err != nil { log.Fatal(err) } defer s.Shutdown(context.Background()) // Use s to send emails. msg, err := email.ComposeTemplatedMessage( email.From("\"John Doe\" <john@doe.com>"), email.To("\"Jane Doe\" <jane@doe.com>"), email.Subject("Hello, World!"), email.Template("welcome", "v1", map[string]any{"name": "John Doe"}), ) if err != nil { log.Fatal(err) } s.Send(context.Background(), msg) }
Output: