fix: display german umlauts correctly in emails

alpha
alex 2023-01-04 00:04:07 +01:00
parent 5360b080b3
commit 5bd3403928
2 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package escaper
import "strings"
var htmlEntitesEscaper = strings.NewReplacer(
`Ä`, "Ä",
`ä`, "ä",
`Ö`, "Ö",
`ö`, "ö",
`Ü`, "Ü",
`ü`, "ü",
)
func EscapeHtmlEntites(s string) string {
return htmlEntitesEscaper.Replace(s)
}

View File

@ -2,12 +2,14 @@ package structs
import ( import (
"bytes" "bytes"
"encoding/base64"
"html/template" "html/template"
"net/smtp" "net/smtp"
"strings" "strings"
"clickandjoin.app/emailserver/modules/cache" "clickandjoin.app/emailserver/modules/cache"
"clickandjoin.app/emailserver/modules/config" "clickandjoin.app/emailserver/modules/config"
"clickandjoin.app/emailserver/modules/escaper"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -70,10 +72,21 @@ func (m *Mail) RenderTemplate() (string, error) {
v = value[config.Cfg.DefaultLanguageCode] 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)) 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)) + "?="
}
t := template.Must(template.New("").Parse(string(body))) t := template.Must(template.New("").Parse(string(body)))
buf := new(bytes.Buffer) buf := new(bytes.Buffer)