diff --git a/services/container.go b/services/container.go index 4e715d5..b0117f3 100644 --- a/services/container.go +++ b/services/container.go @@ -37,9 +37,9 @@ func NewContainer() *Container { c.initCache() c.initDatabase() c.initORM() - c.initMail() c.initAuth() c.initTemplateRenderer() + c.initMail() return c } @@ -135,14 +135,6 @@ func (c *Container) initORM() { } } -func (c *Container) initMail() { - var err error - c.Mail, err = NewMailClient(c.Config) - if err != nil { - panic(fmt.Sprintf("failed to create mail client: %v", err)) - } -} - func (c *Container) initAuth() { c.Auth = NewAuthClient(c.Config, c.ORM) } @@ -150,3 +142,11 @@ func (c *Container) initAuth() { func (c *Container) initTemplateRenderer() { c.Templates = NewTemplateRenderer(c.Config) } + +func (c *Container) initMail() { + var err error + c.Mail, err = NewMailClient(c.Config, c.Templates) + if err != nil { + panic(fmt.Sprintf("failed to create mail client: %v", err)) + } +} diff --git a/services/mail.go b/services/mail.go index 14dfccc..a907b56 100644 --- a/services/mail.go +++ b/services/mail.go @@ -1,6 +1,8 @@ package services import ( + "fmt" + "goweb/config" "github.com/labstack/echo/v4" @@ -13,12 +15,15 @@ import ( type MailClient struct { // config stores application configuration config *config.Config + + templates *TemplateRenderer } // NewMailClient creates a new MailClient -func NewMailClient(cfg *config.Config) (*MailClient, error) { +func NewMailClient(cfg *config.Config, templates *TemplateRenderer) (*MailClient, error) { return &MailClient{ - config: cfg, + config: cfg, + templates: templates, }, nil } @@ -40,6 +45,24 @@ func (c *MailClient) SendTemplate(ctx echo.Context, to, template string, data in ctx.Logger().Debugf("skipping template email sent to: %s") } + // Parse the template, if needed + if err := c.templates.Parse( + "mail", + template, + template, + []string{fmt.Sprintf("email/%s", template)}, + []string{}, + ); err != nil { + return err + } + + // Execute the template + // Uncomment the first variable when ready to use + _, err := c.templates.Execute("mail", template, template, data) + if err != nil { + return err + } + // TODO: Finish based on your mail sender of choice return nil } diff --git a/services/templates.go b/services/templates.go index e72468f..28f34e5 100644 --- a/services/templates.go +++ b/services/templates.go @@ -47,9 +47,9 @@ func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer { func (t *TemplateRenderer) Parse(module, key, name string, files []string, directories []string) error { cacheKey := t.getCacheKey(module, key) - // Check if the template has not yet been parsed or if the app environment is local, so that templates reflect - // changes without having the restart the server - if _, err := t.Load(module, key); err != nil { + // Check if the template has not yet been parsed or if the app environment is local, so that + // templates reflect changes without having the restart the server + if _, err := t.Load(module, key); err != nil || t.config.App.Environment == config.EnvLocal { // Initialize the parsed template with the function map parsed := template.New(name + config.TemplateExt). Funcs(t.funcMap) diff --git a/templates/emails/test.gohtml b/templates/emails/test.gohtml new file mode 100644 index 0000000..189fd91 --- /dev/null +++ b/templates/emails/test.gohtml @@ -0,0 +1 @@ +Test email template. See services/mail.go to provide your implementation. \ No newline at end of file