From 4fb43d081c366c46c02cf6d85ef3860f54ab812b Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Fri, 24 Dec 2021 13:31:33 +0800 Subject: [PATCH] update --- email/base.gohtml | 34 ++++++++++++++++++++++++++++++++++ email/email.go | 21 +++++++++++++++++++++ email/email_test.go | 25 +++++++++++++++++++++++++ email/engine.go | 6 ++++++ 4 files changed, 86 insertions(+) diff --git a/email/base.gohtml b/email/base.gohtml index bbc0e99..f30654c 100644 --- a/email/base.gohtml +++ b/email/base.gohtml @@ -294,6 +294,39 @@ {{ end }} {{ end }} + {{/* actions */}} + {{with .Actions}} + {{if gt (len .) 0}} + {{range $action := .}} +

{{ $action.Instructions }}

+ {{/* count width */}} + {{ $length := len $action.Button.Text }} + {{ $width := add (mul $length 9) 20 }} + {{if (lt $width 200)}}{{$width = 200}}{{else if (gt $width 570)}}{{$width = 570}}{{else}}{{end}} + + {{/* button */}} + + + + +
+
+ {{ if $action.Button.Text }} + + {{ $action.Button.Text }} + + {{end}} + {{ if $action.InviteCode }} + {{ $action.InviteCode }} + {{end}} +
+
+ {{end}} + {{end}} + {{end}} + {{/* Main Body */}} {{ if (ne .Content "") }} {{ .Content | ToHTML }} @@ -314,6 +347,7 @@
{{ if .Sender }}{{.Sender}}{{else}}{{.App.Name}}{{end}}

+ diff --git a/email/email.go b/email/email.go index e393c96..7ddd31a 100644 --- a/email/email.go +++ b/email/email.go @@ -21,6 +21,25 @@ type App struct { Copyright string } +type Button struct { + Text string + Link string + + // Color of the button, default blue(#3869D4) + Color string + // TextColor of the button, default blue(#ffffff) + TextColor string +} + +type Action struct { + // Instructions text show before button + Instructions string + + // InviteCode should not be used with button + InviteCode string + Button Button +} + type Body struct { // App replaced with global config App App @@ -29,6 +48,8 @@ type Body struct { Content string Outros []string + Actions []Action + // Greeting ignored if empty Greeting string // Receiver ignored if Greeting or Receiver empty diff --git a/email/email_test.go b/email/email_test.go index b72c28f..c9d94f9 100644 --- a/email/email_test.go +++ b/email/email_test.go @@ -10,6 +10,31 @@ func TestSendSimple(t *testing.T) { []string{"test@kumoly.io"}, "TestSendSimple", &Body{ Content: `Testing`, + Actions: []Action{ + {Instructions: "click", + Button: Button{ + Text: "click me", Link: "kumoly.io", + }, + }, + {Instructions: "long button", + Button: Button{ + Text: `long long +longlonglonglonglonglonglonglonglonglong +longlonglonglonglonglonglonglonglonglong +longlonglonglonglonglonglonglonglonglong +text`, + Link: "kumoly.io", + Color: "#90CAF9", + TextColor: "#000000", + }, + }, + {Instructions: "None", + Button: Button{}, + }, + {Instructions: "click", + InviteCode: "12354", + }, + }, }, ) if err != nil { diff --git a/email/engine.go b/email/engine.go index 785536d..9703fae 100644 --- a/email/engine.go +++ b/email/engine.go @@ -23,6 +23,12 @@ var mdParser = goldmark.New( func init() { engine = template.Must(template.New("email").Funcs(template.FuncMap{ "ToHTML": ToHTML, + "add": func(a, b int) int { + return a + b + }, + "mul": func(a, b int) int { + return a * b + }, }).Parse(mailTmpl)) }