2021-04-05 23:57:57 +03:00
|
|
|
|
package mail
|
|
|
|
|
|
|
|
|
|
import (
|
2021-04-10 01:07:13 +03:00
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"text/template"
|
2021-04-11 13:42:43 +03:00
|
|
|
|
|
|
|
|
|
conf "github.com/muety/wakapi/config"
|
|
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
|
"github.com/muety/wakapi/views"
|
2021-04-05 23:57:57 +03:00
|
|
|
|
)
|
|
|
|
|
|
2021-04-10 01:07:13 +03:00
|
|
|
|
const (
|
2021-04-10 11:48:06 +03:00
|
|
|
|
tplNamePasswordReset = "reset_password"
|
|
|
|
|
tplNameImportNotification = "import_finished"
|
|
|
|
|
subjectPasswordReset = "Wakapi – Password Reset"
|
|
|
|
|
subjectImportNotification = "Wakapi – Data Import Finished"
|
2021-04-10 01:07:13 +03:00
|
|
|
|
)
|
|
|
|
|
|
2021-04-10 11:48:06 +03:00
|
|
|
|
type PasswordResetTplData struct {
|
2021-04-10 01:07:13 +03:00
|
|
|
|
ResetLink string
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 11:48:06 +03:00
|
|
|
|
type ImportNotificationTplData struct {
|
|
|
|
|
PublicUrl string
|
|
|
|
|
Duration string
|
|
|
|
|
NumHeartbeats int
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 01:07:13 +03:00
|
|
|
|
// 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 {
|
2021-04-10 11:48:06 +03:00
|
|
|
|
return NewMailWhaleService(config.Mail.MailWhale, config.Server.PublicUrl)
|
2021-04-10 01:07:13 +03:00
|
|
|
|
} else if config.Mail.Provider == conf.MailProviderSmtp {
|
2021-04-10 11:48:06 +03:00
|
|
|
|
return NewSMTPMailService(config.Mail.Smtp, config.Server.PublicUrl)
|
2021-04-10 01:07:13 +03:00
|
|
|
|
}
|
2021-04-05 23:57:57 +03:00
|
|
|
|
}
|
|
|
|
|
return &NoopMailService{}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 11:48:06 +03:00
|
|
|
|
func getPasswordResetTemplate(data PasswordResetTplData) (*bytes.Buffer, error) {
|
2021-04-10 01:07:13 +03:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 11:48:06 +03:00
|
|
|
|
func getImportNotificationTemplate(data ImportNotificationTplData) (*bytes.Buffer, error) {
|
|
|
|
|
tpl, err := loadTemplate(tplNameImportNotification)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var rendered bytes.Buffer
|
|
|
|
|
if err := tpl.Execute(&rendered, data); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &rendered, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 01:07:13 +03:00
|
|
|
|
func loadTemplate(tplName string) (*template.Template, error) {
|
2021-04-11 13:42:43 +03:00
|
|
|
|
tplFile, err := views.TemplateFiles.Open(fmt.Sprintf("mail/%s.tpl.html", tplName))
|
2021-04-10 01:07:13 +03:00
|
|
|
|
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
|
|
|
|
}
|