112 lines
2.9 KiB
Go
112 lines
2.9 KiB
Go
package structs
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"html/template"
|
|
"net/smtp"
|
|
"strings"
|
|
"time"
|
|
|
|
"clickandjoin.app/emailserver/modules/cache"
|
|
"clickandjoin.app/emailserver/modules/config"
|
|
"clickandjoin.app/emailserver/modules/escaper"
|
|
gocnjhelper "git.ex.umbach.dev/ClickandJoin/go-cnj-helper"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Mail struct {
|
|
To []string
|
|
Subject string
|
|
TemplateId string
|
|
LanguageId string
|
|
BodyData interface{}
|
|
}
|
|
|
|
func (m *Mail) Send(body string) error {
|
|
cfg := config.Cfg.Mail
|
|
|
|
msg := "From: " + EncodeBase64(cfg.FromName) + " <" + cfg.FromEmail + ">\n" +
|
|
"To: " + strings.Join(m.To, ",") + "\n" +
|
|
"Subject: " + m.Subject + "\n" +
|
|
"Date: " + time.Now().Format("Mon, 02 Jan 2006 15:04:05 -0700") + "\n" +
|
|
"Message-ID: <" + uuid.New().String() + "@" + strings.Split(cfg.FromEmail, "@")[1] + ">\n" +
|
|
"MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" +
|
|
body
|
|
|
|
err := smtp.SendMail(cfg.Host+":"+cfg.Port, cache.SmtpAuth, cfg.FromEmail, m.To, []byte(msg))
|
|
|
|
if err != nil {
|
|
gocnjhelper.LogErrorf("smtp error: %s", err)
|
|
return err
|
|
}
|
|
|
|
gocnjhelper.LogDebugf("SEND MAIL %s", msg)
|
|
return nil
|
|
}
|
|
|
|
func (m *Mail) RenderTemplate() (string, error) {
|
|
body := cache.BodyTemplates[m.TemplateId]
|
|
|
|
for templateName, templateData := range cache.Templates.Templates {
|
|
// Skipping templates if the template ID is not the expected one
|
|
if templateName != m.TemplateId {
|
|
continue
|
|
}
|
|
|
|
gocnjhelper.LogDebugf("RENDER TEMPLATE %s %s", templateName, templateData)
|
|
|
|
m.Subject = templateData["mailSubject"][m.LanguageId]
|
|
|
|
// occurs if the requested language code does not exist
|
|
if m.Subject == "" {
|
|
m.Subject = templateData["mailSubject"][config.Cfg.DefaultLanguageCode]
|
|
}
|
|
|
|
// replace body %values% with values in templates config
|
|
for key, value := range templateData {
|
|
if key == "mailSubject" {
|
|
continue
|
|
}
|
|
|
|
v := value[m.LanguageId]
|
|
|
|
// occurs if the requested language code does not exist
|
|
if v == "" {
|
|
v = value[config.Cfg.DefaultLanguageCode]
|
|
}
|
|
|
|
// escaping so that umlauts are displayed correctly
|
|
if m.LanguageId == "de" {
|
|
v = escaper.EscapeHtmlEntites(v)
|
|
}
|
|
|
|
body = []byte(strings.Replace(string(body), "%"+key+"%", v, -1))
|
|
}
|
|
}
|
|
|
|
// The subject line of an email is an independent header and utf 8 must be set for it
|
|
// https://ncona.com/2011/06/using-utf-8-characters-on-an-e-mail-subject/
|
|
if m.LanguageId == "de" {
|
|
//m.Subject = "=?utf-8?B?" + base64.StdEncoding.EncodeToString([]byte(m.Subject)) + "?="
|
|
|
|
m.Subject = EncodeBase64(m.Subject)
|
|
}
|
|
|
|
t := template.Must(template.New("").Parse(string(body)))
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := t.Execute(buf, m.BodyData); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return buf.String(), nil
|
|
}
|
|
|
|
// https://ncona.com/2011/06/using-utf-8-characters-on-an-e-mail-subject/
|
|
func EncodeBase64(s string) string {
|
|
return fmt.Sprintf("=?utf-8?B?%s?=", base64.StdEncoding.EncodeToString([]byte(s)))
|
|
}
|