2021-04-05 23:57:57 +03:00
|
|
|
|
package mail
|
|
|
|
|
|
|
|
|
|
import (
|
2021-04-10 01:07:13 +03:00
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/markbates/pkger"
|
2021-04-05 23:57:57 +03:00
|
|
|
|
conf "github.com/muety/wakapi/config"
|
|
|
|
|
"github.com/muety/wakapi/services"
|
2021-04-10 01:07:13 +03:00
|
|
|
|
"io/ioutil"
|
|
|
|
|
"text/template"
|
2021-04-05 23:57:57 +03:00
|
|
|
|
)
|
|
|
|
|
|
2021-04-10 01:07:13 +03:00
|
|
|
|
const (
|
|
|
|
|
tplPath = "/views/mail"
|
|
|
|
|
tplNamePasswordReset = "reset_password"
|
|
|
|
|
subjectPasswordReset = "Wakapi – Password Reset"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type passwordResetLinkTplData struct {
|
|
|
|
|
ResetLink string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Factory
|
2021-04-05 23:57:57 +03:00
|
|
|
|
func NewMailService() services.IMailService {
|
|
|
|
|
config := conf.Get()
|
2021-04-10 01:07:13 +03:00
|
|
|
|
if config.Mail.Enabled {
|
|
|
|
|
if config.Mail.Provider == conf.MailProviderMailWhale {
|
|
|
|
|
return NewMailWhaleService(config.Mail.MailWhale)
|
|
|
|
|
} else if config.Mail.Provider == conf.MailProviderSmtp {
|
|
|
|
|
return NewSMTPMailService(config.Mail.Smtp)
|
|
|
|
|
}
|
2021-04-05 23:57:57 +03:00
|
|
|
|
}
|
|
|
|
|
return &NoopMailService{}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 01:07:13 +03:00
|
|
|
|
func getPasswordResetTemplate(data passwordResetLinkTplData) (*bytes.Buffer, error) {
|
|
|
|
|
tpl, err := loadTemplate(tplNamePasswordReset)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var rendered bytes.Buffer
|
|
|
|
|
if err := tpl.Execute(&rendered, data); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &rendered, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func loadTemplate(tplName string) (*template.Template, error) {
|
|
|
|
|
tplFile, err := pkger.Open(fmt.Sprintf("%s/%s.tpl.html", tplPath, tplName))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer tplFile.Close()
|
|
|
|
|
|
|
|
|
|
tplData, err := ioutil.ReadAll(tplFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-04-05 23:57:57 +03:00
|
|
|
|
|
2021-04-10 01:07:13 +03:00
|
|
|
|
return template.New(tplName).Parse(string(tplData))
|
2021-04-05 23:57:57 +03:00
|
|
|
|
}
|