feat: update mail template

This commit is contained in:
Zhang Minghan 2023-12-24 07:53:30 +08:00
parent a82c5be593
commit c0f382b47a
3 changed files with 77 additions and 1 deletions

View File

@ -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,
)
}

View File

@ -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 {

52
utils/templates/code.html Normal file
View File

@ -0,0 +1,52 @@
<link href="https://fonts.googlefonts.cn/css?family=Open+Sans" rel="stylesheet">
<style>
* {
font-family: "Open Sans", Ubuntu, Verdana, Nunito, monospace, Consolas, Monospace, sans-serif;
}
.im { /* gmail adapter */
color: inherit;
}
.main {
width: max-content;
padding: 60px 35px;
border: 1px solid lightgray;
border-radius: 10px;
margin: 10px auto;
}
.column {
text-align: center;
}
h1 {
margin-top: 4px;
}
a {
text-decoration: none;
transition: .5s;
color: #009efd;
}
a:active, a:hover {
color: #0d64fd;
}
img {
width: 64px;
height: 64px;
}
.code {
color: #58a6ff;
font-size: large;
}
</style>
<body>
<div class="main">
<div class="column"><img src="https://chatnio.net/favicon.ico" alt=""><h1>Chat Nio</h1></div>
<div class="column"><p>Your One-Time Password is <strong class="code">{{.Code}}</strong></p></div>
<div class="column" style="color:gray">
<span>The code will expire in <strong>10 minutes</strong>.</span><br>
<span>If it is not operated by yourself, please ignore it.</span><br>
</div>
<br>
<div class="column">
<a href="">&copy; Chat Nio</a>
</div>
</div>
</body>