commento/api/smtp_templates.go

56 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"fmt"
2018-06-11 01:43:42 +08:00
"os"
"text/template"
)
var headerTemplate *template.Template
type headerPlugs struct {
FromAddress string
ToName string
ToAddress string
Subject string
}
var templates map[string]*template.Template
2018-06-09 17:50:01 +08:00
func smtpTemplatesLoad() error {
var err error
headerTemplate, err = template.New("header").Parse(`MIME-Version: 1.0
From: Commento <{{.FromAddress}}>
To: {{.ToName}} <{{.ToAddress}}>
Content-Type: text/plain; charset=UTF-8
Subject: {{.Subject}}
`)
if err != nil {
2018-06-09 16:32:07 +08:00
logger.Errorf("cannot parse header template: %v", err)
return errorMalformedTemplate
}
2019-02-19 00:23:44 +08:00
names := []string{
"confirm-hex",
"reset-hex",
"domain-export",
"domain-export-error",
}
templates = make(map[string]*template.Template)
logger.Infof("loading templates: %v", names)
for _, name := range names {
var err error
templates[name] = template.New(name)
templates[name], err = template.ParseFiles(fmt.Sprintf("%s/templates/%s.txt", os.Getenv("STATIC"), name))
if err != nil {
logger.Errorf("cannot parse %s/templates/%s.txt: %v", os.Getenv("STATIC"), name, err)
return errorMalformedTemplate
}
}
return nil
}