Moved mail to services package.

This commit is contained in:
mikestefanello 2021-12-19 16:22:55 -05:00
parent af645ee2e4
commit 28bda89863
3 changed files with 53 additions and 27 deletions

View File

@ -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?

50
services/mail.go Normal file
View File

@ -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
}

3
services/mail_test.go Normal file
View File

@ -0,0 +1,3 @@
package services
// Fill this in once you implement your mail client