diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go index d5c7b22..8e760b4 100644 --- a/modules/mailer/mailer.go +++ b/modules/mailer/mailer.go @@ -14,32 +14,32 @@ import ( ) var auth smtp.Auth -var cfg = &config.Cfg.Mail +var cfg = &config.Cfg func InitMailer() { - auth = smtp.PlainAuth("", cfg.User, cfg.Password, cfg.Host) + auth = smtp.PlainAuth("", cfg.Mail.User, cfg.Mail.Password, cfg.Mail.Host) readTemplatesConfig() loadTemplateFiles() } type Mail struct { - To []string - Subject string - BodyTemplateId int - LanguageId int - BodyData interface{} + To []string + Subject string + TemplateId int + LanguageId int + BodyData interface{} } -func NewMail(to []string, bodyTemplateId int, languageId int, bodyData interface{}) { - log.Infoln("new mail", bodyTemplateId, bodyData) +func NewMail(to []string, templateId int, languageId int, bodyData interface{}) { + log.Infoln("new mail", templateId, bodyData) mail := Mail{ - To: to, - Subject: "", - BodyTemplateId: bodyTemplateId, - LanguageId: languageId, - BodyData: bodyData, + To: to, + Subject: "", + TemplateId: templateId, + LanguageId: languageId, + BodyData: bodyData, } body, err := mail.RenderTemplate() @@ -54,13 +54,13 @@ func NewMail(to []string, bodyTemplateId int, languageId int, bodyData interface } func (m *Mail) Send(body string) { - msg := "From: " + cfg.From + "\n" + + msg := "From: " + cfg.Mail.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)) + err := smtp.SendMail(cfg.Mail.Host+":"+cfg.Mail.Port, auth, cfg.Mail.From, m.To, []byte(msg)) if err != nil { log.Warnln("smtp error:", err) @@ -74,14 +74,14 @@ type Templates struct { } type Template struct { - Id int `json:"id"` + Id int `json:"_id"` FileName string `json:"fileName"` Body string Languages []Languages `json:"languages"` } type Languages struct { - Id int `json:"id"` + Id int `json:"_id"` Subject string `json:"subject"` Texts map[string]interface{} `json:"texts"` } @@ -89,7 +89,7 @@ type Languages struct { var templates Templates func readTemplatesConfig() { - byteValue, err := ioutil.ReadFile("./templates/templates.json") + byteValue, err := ioutil.ReadFile(cfg.Templates.ConfigPath) if err != nil { log.Fatal(err) @@ -98,53 +98,65 @@ func readTemplatesConfig() { err = json.Unmarshal(byteValue, &templates) if err != nil { - log.Fatal("failed unmarshal", err) + log.Fatal("Failed to unmarshal templates config", err) } - log.Infoln("templates", templates) + log.Debug("Checking the order of the ids in the template json file ...") + + for t := 0; t < len(templates.Templates); t++ { + if templates.Templates[t].Id != t { + log.Fatalln("Id", templates.Templates[t].Id, "should be templateId", t) + } + for l := 0; l < len(templates.Templates[t].Languages); l++ { + if templates.Templates[t].Languages[l].Id != l { + log.Fatal("TemplateId: ", t, " -> Id", templates.Templates[t].Languages[l].Id, "should be languageId", l) + } + } + } + + log.Debug("Template Ids all correct") } func loadTemplateFiles() { - for i := 0; i < len(templates.Templates); i++ { - log.Debugln("load template file id", templates.Templates[i].Id) + log.Debugln("Starting the import of template files and their intermediate storage ...") + for i := 0; i < len(templates.Templates); i++ { t := &templates.Templates[i] - data, err := ioutil.ReadFile(cfg.TemplatePath + t.FileName + ".html") + data, err := ioutil.ReadFile(cfg.Templates.FolderPath + t.FileName + ".html") if err != nil { - log.Fatalln("read file err", err) + log.Fatalln("Failed to read template file -> id:", templates.Templates[i].Id, "name:", templates.Templates[i].FileName, "err:", err) } t.Body = string(data) } + + log.Debugln("Importing of template files finish") } func (m *Mail) RenderTemplate() (string, error) { - // define subject form config - m.Subject = templates.Templates[0].Languages[0].Subject + // define subject from config + m.Subject = templates.Templates[m.TemplateId].Languages[m.LanguageId].Subject - body := templates.Templates[0].Body + body := templates.Templates[m.TemplateId].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) + for key, value := range templates.Templates[m.TemplateId].Languages[m.LanguageId].Texts { + strKey := fmt.Sprintf("%v", key) + strValue := fmt.Sprintf("%v", value) - log.Infoln("a", strKey, strValue) + log.Infoln("a", strKey, strValue) - body = strings.Replace(body, "%"+strKey+"%", strValue, -1) - } + 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) + log.Fatalln("Error executing body data", err) } return buf.String(), nil