From b8c2ec315e7602e00309c1c061cb9369de604d19 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 20 Jan 2024 22:54:39 +0100 Subject: [PATCH] disabled html minfiy, added some headers and new templateS --- go.mod | 1 + go.sum | 2 + mailer/template.go | 13 ++-- modules/config/config.go | 22 +++--- modules/rabbitmq/helper.go | 3 +- modules/structs/mail.go | 18 ++++- templates/emailVerification2.html | 119 ++++++++++++++++++++++++++++++ templates/emailVerifyFailed.html | 96 ++++++++++++++++++++++++ templates/templates.json | 72 ++++++++++++++++++ 9 files changed, 326 insertions(+), 20 deletions(-) create mode 100644 templates/emailVerification2.html create mode 100644 templates/emailVerifyFailed.html diff --git a/go.mod b/go.mod index 2c9af9a..6bda77f 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( require ( github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1 // indirect github.com/golang/snappy v0.0.4 // indirect + github.com/google/uuid v1.5.0 // indirect github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect diff --git a/go.sum b/go.sum index d845d7a..871e93d 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,8 @@ github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= diff --git a/mailer/template.go b/mailer/template.go index a64c0fc..5866b1c 100644 --- a/mailer/template.go +++ b/mailer/template.go @@ -7,7 +7,6 @@ import ( "clickandjoin.app/emailserver/modules/cache" "clickandjoin.app/emailserver/modules/config" gocnjhelper "git.ex.umbach.dev/ClickandJoin/go-cnj-helper" - "github.com/tdewolff/minify/v2/minify" ) func readTemplatesConfig() { @@ -37,14 +36,16 @@ func loadTemplateFiles() { if err != nil { gocnjhelper.LogFatalf("Failed to read file, err: %s", err) } + /* + minifiedHtml, err := minify.HTML(string(data)) - minifiedHtml, err := minify.HTML(string(data)) + if err != nil { + gocnjhelper.LogFatalf("Failed to minify html, err: %s", err) + } */ - if err != nil { - gocnjhelper.LogFatalf("Failed to minify html, err: %s", err) - } + // cache.BodyTemplates[templateName] = []byte(minifiedHtml) - cache.BodyTemplates[templateName] = []byte(minifiedHtml) + cache.BodyTemplates[templateName] = []byte(data) } gocnjhelper.LogDebug("FINISHED IMPORTING TEMPLATE FILES") diff --git a/modules/config/config.go b/modules/config/config.go index c43e2ec..e684eab 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -27,11 +27,12 @@ type RabbitMq struct { } type Mail struct { - Host string - Username string - Password string - Port string - From string + Host string + Username string + Password string + Port string + FromName string + FromEmail string } type Templates struct { @@ -77,11 +78,12 @@ func LoadConfig() { Password: os.Getenv("RABBITMQ_PASSWORD"), }, Mail: Mail{ - Host: os.Getenv("MAIL_HOST"), - Username: os.Getenv("MAIL_USERNAME"), - Password: os.Getenv("MAIL_PASSWORD"), - Port: os.Getenv("MAIL_PORT"), - From: os.Getenv("MAIL_FROM"), + Host: os.Getenv("MAIL_HOST"), + Username: os.Getenv("MAIL_USERNAME"), + Password: os.Getenv("MAIL_PASSWORD"), + Port: os.Getenv("MAIL_PORT"), + FromName: os.Getenv("MAIL_FROM_NAME"), + FromEmail: os.Getenv("MAIL_FROM_EMAIL"), }, Templates: Templates{ FolderPath: os.Getenv("TEMPLATES_FOLDER_PATH"), diff --git a/modules/rabbitmq/helper.go b/modules/rabbitmq/helper.go index 5b2eb61..037bade 100644 --- a/modules/rabbitmq/helper.go +++ b/modules/rabbitmq/helper.go @@ -58,7 +58,8 @@ func MailMessagesHandling() { // only for testing if !strings.HasSuffix(mailMessage.UserMail, "@roese.dev") && !strings.HasSuffix(mailMessage.UserMail, "@umbach.dev") && - !strings.HasSuffix(mailMessage.UserMail, "@kk-innovation.eu") { + !strings.HasSuffix(mailMessage.UserMail, "@kk-innovation.eu") && + !strings.HasSuffix(mailMessage.UserMail, "@srv1.mail-tester.com") { mailMessage.UserMail = "myprojects@roese.dev" } diff --git a/modules/structs/mail.go b/modules/structs/mail.go index 2648f7f..9462e1a 100644 --- a/modules/structs/mail.go +++ b/modules/structs/mail.go @@ -3,14 +3,17 @@ 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 { @@ -24,13 +27,15 @@ type Mail struct { func (m *Mail) Send(body string) error { cfg := config.Cfg.Mail - msg := "From: " + cfg.From + "\n" + + 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.From, m.To, []byte(msg)) + err := smtp.SendMail(cfg.Host+":"+cfg.Port, cache.SmtpAuth, cfg.FromEmail, m.To, []byte(msg)) if err != nil { gocnjhelper.LogErrorf("smtp error: %s", err) @@ -84,7 +89,9 @@ func (m *Mail) RenderTemplate() (string, error) { // 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 = "=?utf-8?B?" + base64.StdEncoding.EncodeToString([]byte(m.Subject)) + "?=" + + m.Subject = EncodeBase64(m.Subject) } t := template.Must(template.New("").Parse(string(body))) @@ -97,3 +104,8 @@ func (m *Mail) RenderTemplate() (string, error) { 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))) +} diff --git a/templates/emailVerification2.html b/templates/emailVerification2.html new file mode 100644 index 0000000..200bf33 --- /dev/null +++ b/templates/emailVerification2.html @@ -0,0 +1,119 @@ + + + + + + + + + + + + + +
+

%title%

+

%header%

+

%text1%

+ %verifyButton% +
+

%cancelText%

+ %cancelButton% +
+ + + + +
+

%yourAppointment%

+

%appointment1%

+

%appointment2%

+

%appointment3%

+
+
+

{{.address}}

+ + + %dsgvo% +
+ + diff --git a/templates/emailVerifyFailed.html b/templates/emailVerifyFailed.html new file mode 100644 index 0000000..198215b --- /dev/null +++ b/templates/emailVerifyFailed.html @@ -0,0 +1,96 @@ + + + + + + + + + + + + + +
+

%title%

+

%header%

+

%text1%

+
+ + + + +
+

%yourAppointment%

+

%appointment1%

+

%appointment2%

+

%appointment3%

+
+
+

{{.address}}

+ + %dsgvo% +
+ + diff --git a/templates/templates.json b/templates/templates.json index a6a227d..f166885 100644 --- a/templates/templates.json +++ b/templates/templates.json @@ -18,6 +18,78 @@ "de": "Bitte gebe diesen Code in der App ein" } }, + "emailVerifyFailed": { + "mailSubject": { + "de": "Ihre Buchung wurde storniert" + }, + "title": { + "de": "Ihre Buchung wurde storniert" + }, + "header": { + "de": "Liebe(r) {{.name}}," + }, + "text1": { + "de": "Ihre Buchung wurde storniert. Sie können gerne einen neuen Termin auf unserer Webseite festlegen." + }, + "yourAppointment": { + "de": "Ihr Termin:" + }, + "appointment1": { + "de": "{{.activityName}} bei {{.username}}," + }, + "appointment2": { + "de": "{{.day}}, {{.dayNumber}}. {{.month}} {{.year}}" + }, + "appointment3": { + "de": "von {{.startTime}} bis {{.endTime}} Uhr" + }, + "footer": { + "de": "Vielen Dank, dass Sie unseren Service gewählt haben. Bei Fragen kontaktieren Sie uns gerne." + }, + "dsgvo": { + "de": "Datenschutzerklärung" + } + }, + "emailVerification2": { + "mailSubject": { + "de": "Bitte Bestätigen Sie Ihre Buchung" + }, + "title": { + "de": "Buchungsbestätigung" + }, + "header": { + "de": "Liebe(r) {{.name}}," + }, + "text1": { + "de": "Ihre Buchung wurde erfolgreich empfangen und wartet auf Bestätigung. Bitte klicken Sie auf die untenstehende Schaltfläche, um den Vorgang abzuschließen." + }, + "verifyButton": { + "de": "Buchung bestätigen" + },"cancelButton": { + "de": "Buchung stornieren" + }, + "cancelText": { + "de": "Falls Sie Ihren Termin nicht wahrnehmen können, bitten wir Sie, den Termin zu stornieren." + }, + "yourAppointment": { + "de": "Ihr Termin:" + }, + "appointment1": { + "de": "{{.activityName}} bei {{.username}}," + }, + "appointment2": { + "de": "{{.day}}, {{.dayNumber}}. {{.month}} {{.year}}" + }, + "appointment3": { + "de": "von {{.startTime}} bis {{.endTime}} Uhr" + }, + "footer": { + "de": "Vielen Dank, dass Sie unseren Service gewählt haben. Bei Fragen kontaktieren Sie uns gerne." + }, + "dsgvo": { + "de": "Datenschutzerklärung" + } + }, "newUserSignIn": { "mailSubject": { "en": "A new sign-in was detected",