152 lines
3.1 KiB
Go
152 lines
3.1 KiB
Go
package mailer
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"html/template"
|
|
"io/ioutil"
|
|
"net/smtp"
|
|
"strings"
|
|
|
|
"git.umbach.dev/app-idea/mailer/modules/config"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var auth smtp.Auth
|
|
var cfg = &config.Cfg.Mail
|
|
|
|
func InitMailer() {
|
|
auth = smtp.PlainAuth("", cfg.User, cfg.Password, cfg.Host)
|
|
|
|
readTemplatesConfig()
|
|
loadTemplateFiles()
|
|
}
|
|
|
|
type Mail struct {
|
|
To []string
|
|
Subject string
|
|
BodyTemplateId int
|
|
LanguageId int
|
|
BodyData interface{}
|
|
}
|
|
|
|
func NewMail(to []string, bodyTemplateId int, languageId int, bodyData interface{}) {
|
|
log.Infoln("new mail", bodyTemplateId, bodyData)
|
|
|
|
mail := Mail{
|
|
To: to,
|
|
Subject: "",
|
|
BodyTemplateId: bodyTemplateId,
|
|
LanguageId: languageId,
|
|
BodyData: bodyData,
|
|
}
|
|
|
|
body, err := mail.RenderTemplate()
|
|
|
|
log.Infoln("body", body, bodyData)
|
|
|
|
if err != nil {
|
|
log.Fatalln("error parsing", err)
|
|
}
|
|
|
|
mail.Send(body)
|
|
}
|
|
|
|
func (m *Mail) Send(body string) {
|
|
msg := "From: " + cfg.From + "\n" +
|
|
"To: " + strings.Join(m.To, ",") + "\n" +
|
|
"Subject: " + m.Subject + "\n" +
|
|
"MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" +
|
|
body
|
|
|
|
err := smtp.SendMail(cfg.Host+":"+cfg.Port, auth, cfg.From, m.To, []byte(msg))
|
|
|
|
if err != nil {
|
|
log.Warnln("smtp error:", err)
|
|
} else {
|
|
log.Debugln("send mail")
|
|
}
|
|
}
|
|
|
|
type Templates struct {
|
|
Templates []Template `json:"templates"`
|
|
}
|
|
|
|
type Template struct {
|
|
Id int `json:"id"`
|
|
FileName string `json:"fileName"`
|
|
Body string
|
|
Languages []Languages `json:"languages"`
|
|
}
|
|
|
|
type Languages struct {
|
|
Id int `json:"id"`
|
|
Subject string `json:"subject"`
|
|
Texts map[string]interface{} `json:"texts"`
|
|
}
|
|
|
|
var templates Templates
|
|
|
|
func readTemplatesConfig() {
|
|
byteValue, err := ioutil.ReadFile("./templates/templates.json")
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
err = json.Unmarshal(byteValue, &templates)
|
|
|
|
if err != nil {
|
|
log.Fatal("failed unmarshal", err)
|
|
}
|
|
|
|
log.Infoln("templates", templates)
|
|
}
|
|
|
|
func loadTemplateFiles() {
|
|
for i := 0; i < len(templates.Templates); i++ {
|
|
log.Debugln("load template file id", templates.Templates[i].Id)
|
|
|
|
t := &templates.Templates[i]
|
|
|
|
data, err := ioutil.ReadFile(cfg.TemplatePath + t.FileName + ".html")
|
|
|
|
if err != nil {
|
|
log.Fatalln("read file err", err)
|
|
}
|
|
|
|
t.Body = string(data)
|
|
}
|
|
}
|
|
|
|
func (m *Mail) RenderTemplate() (string, error) {
|
|
// define subject form config
|
|
m.Subject = templates.Templates[0].Languages[0].Subject
|
|
|
|
body := templates.Templates[0].Body
|
|
|
|
// replace body %values% with values in templates config
|
|
for i := 0; i < len(templates.Templates[0].Languages); i++ {
|
|
for key, value := range templates.Templates[0].Languages[i].Texts {
|
|
strKey := fmt.Sprintf("%v", key)
|
|
strValue := fmt.Sprintf("%v", value)
|
|
|
|
log.Infoln("a", strKey, strValue)
|
|
|
|
body = strings.Replace(body, "%"+strKey+"%", strValue, -1)
|
|
}
|
|
}
|
|
|
|
t := template.Must(template.New("").Parse(body))
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
log.Infoln("test")
|
|
if err := t.Execute(buf, m.BodyData); err != nil {
|
|
log.Fatalln("err execute", err)
|
|
}
|
|
|
|
return buf.String(), nil
|
|
}
|