From c0f382b47a319a6b301a4830d3926a3c13aa00c0 Mon Sep 17 00:00:00 2001 From: Zhang Minghan Date: Sun, 24 Dec 2023 07:53:30 +0800 Subject: [PATCH] feat: update mail template --- auth/auth.go | 10 +++++++- utils/smtp.go | 16 ++++++++++++ utils/templates/code.html | 52 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 utils/templates/code.html diff --git a/auth/auth.go b/auth/auth.go index 4514ef1..cfb2e45 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -94,10 +94,18 @@ func Verify(c *gin.Context, email string) error { code := generateCode(c, cache, email) provider := channel.SystemInstance.GetMail() + + template, err := provider.RenderTemplate("code.html", struct { + Code string + }{Code: code}) + if err != nil { + return err + } + return provider.SendMail( email, "Chat Nio | OTP Verification", - fmt.Sprintf("Your OTP code is: %s", code), + template, ) } diff --git a/utils/smtp.go b/utils/smtp.go index e368c81..9b8a87e 100644 --- a/utils/smtp.go +++ b/utils/smtp.go @@ -1,10 +1,12 @@ package utils import ( + "bytes" "crypto/tls" "fmt" "net" "net/smtp" + "text/template" ) type SmtpPoster struct { @@ -38,6 +40,20 @@ func (s *SmtpPoster) SendMail(to string, subject string, body string) error { }, body))) } +func (s *SmtpPoster) RenderTemplate(filename string, data interface{}) (string, error) { + tmpl, err := template.New(filename).ParseFiles(fmt.Sprintf("utils/templates/%s", filename)) + if err != nil { + return "", err + } + + var buf bytes.Buffer + if err := tmpl.ExecuteTemplate(&buf, filename, data); err != nil { + return "", err + } + + return buf.String(), nil +} + func dial(addr string) (*smtp.Client, error) { conn, err := tls.Dial("tcp", addr, nil) if err != nil { diff --git a/utils/templates/code.html b/utils/templates/code.html new file mode 100644 index 0000000..b0d9dde --- /dev/null +++ b/utils/templates/code.html @@ -0,0 +1,52 @@ + + + +
+

Chat Nio

+

Your One-Time Password is {{.Code}}

+
+ The code will expire in 10 minutes.
+ If it is not operated by yourself, please ignore it.
+
+
+ +
+ \ No newline at end of file