Add template parsing and execution to mail service.

This commit is contained in:
mikestefanello 2021-12-19 20:37:51 -05:00
parent 15974c9b77
commit 337ebb67b4
4 changed files with 38 additions and 14 deletions

View file

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