From 28bda8986339396e55a15a1ecc8e8321d13a4eac Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Sun, 19 Dec 2021 16:22:55 -0500 Subject: [PATCH] Moved mail to services package. --- mail/mail.go | 27 ----------------------- services/mail.go | 50 +++++++++++++++++++++++++++++++++++++++++++ services/mail_test.go | 3 +++ 3 files changed, 53 insertions(+), 27 deletions(-) delete mode 100644 mail/mail.go create mode 100644 services/mail.go create mode 100644 services/mail_test.go diff --git a/mail/mail.go b/mail/mail.go deleted file mode 100644 index 8db46ac..0000000 --- a/mail/mail.go +++ /dev/null @@ -1,27 +0,0 @@ -package mail - -import ( - "goweb/config" - - "github.com/labstack/echo/v4" -) - -type Client struct { - config *config.Config -} - -func NewClient(cfg *config.Config) (*Client, error) { - return &Client{ - config: cfg, - }, nil -} - -func (c *Client) Send(ctx echo.Context, to, body string) error { - if c.config.App.Environment != config.EnvProduction { - // IE, skip sending email.. - } - ctx.Logger().Debugf("Mock email sent. To: %s Body: %s", to, body) - return nil -} - -// TODO: Send with template? diff --git a/services/mail.go b/services/mail.go new file mode 100644 index 0000000..14dfccc --- /dev/null +++ b/services/mail.go @@ -0,0 +1,50 @@ +package services + +import ( + "goweb/config" + + "github.com/labstack/echo/v4" +) + +// MailClient provides a client for sending email +// This is purposely not completed because there are many different methods and services +// for sending email, many of which are very different. Choose what works best for you +// and populate the methods below +type MailClient struct { + // config stores application configuration + config *config.Config +} + +// NewMailClient creates a new MailClient +func NewMailClient(cfg *config.Config) (*MailClient, error) { + return &MailClient{ + config: cfg, + }, nil +} + +// Send sends an email to a given email address with a given body +func (c *MailClient) Send(ctx echo.Context, to, body string) error { + if c.skipSend() { + ctx.Logger().Debugf("skipping email sent to: %s") + } + + // TODO: Finish based on your mail sender of choice + return nil +} + +// SendTemplate sends an email to a given email address using a template and data which is passed to the template +// The template name should only include the filename without the extension or directory. +// The funcmap will be automatically added to the template and the data will be passed in. +func (c *MailClient) SendTemplate(ctx echo.Context, to, template string, data interface{}) error { + if c.skipSend() { + ctx.Logger().Debugf("skipping template email sent to: %s") + } + + // TODO: Finish based on your mail sender of choice + return nil +} + +// skipSend determines if mail sending should be skipped +func (c *MailClient) skipSend() bool { + return c.config.App.Environment != config.EnvProduction +} diff --git a/services/mail_test.go b/services/mail_test.go new file mode 100644 index 0000000..c45c7be --- /dev/null +++ b/services/mail_test.go @@ -0,0 +1,3 @@ +package services + +// Fill this in once you implement your mail client