[优化] 邮件工具类去耦合

This commit is contained in:
ryan
2026-05-31 13:19:47 +08:00
parent 632c455229
commit 9975ac90c4
2 changed files with 38 additions and 15 deletions
+16 -2
View File
@@ -93,7 +93,14 @@ func SendEmailVerification(c *gin.Context) {
content := fmt.Sprintf("<p>您好,你正在进行%s邮箱验证。</p>"+
"<p>您的验证码为: <strong>%s</strong></p>"+
"<p>验证码 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, code, security.VerificationValidMinutes)
err := mail.SendEmail(subject, email, content)
cfg := mail.SMTPConfig{
Server: common.SMTPServer,
Port: common.SMTPPort,
Account: common.SMTPAccount,
Token: common.SMTPToken,
SystemName: common.SystemName,
}
err := mail.SendEmail(cfg, subject, email, content)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
@@ -131,7 +138,14 @@ func SendPasswordResetEmail(c *gin.Context) {
content := fmt.Sprintf("<p>您好,你正在进行%s密码重置。</p>"+
"<p>点击<a href='%s'>此处</a>进行密码重置。</p>"+
"<p>重置链接 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, link, security.VerificationValidMinutes)
err := mail.SendEmail(subject, email, content)
cfg := mail.SMTPConfig{
Server: common.SMTPServer,
Port: common.SMTPPort,
Account: common.SMTPAccount,
Token: common.SMTPToken,
SystemName: common.SystemName,
}
err := mail.SendEmail(cfg, subject, email, content)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
+22 -13
View File
@@ -5,31 +5,40 @@ import (
"encoding/base64"
"fmt"
"net/smtp"
"openflare/common"
"strings"
)
func SendEmail(subject string, receiver string, content string) error {
// SMTPConfig holds all the configuration parameters required to send an email.
type SMTPConfig struct {
Server string
Port int
Account string
Token string
SystemName string
}
// SendEmail sends an HTML email to the receiver using the provided SMTP configuration.
func SendEmail(config SMTPConfig, subject string, receiver string, content string) error {
encodedSubject := fmt.Sprintf("=?UTF-8?B?%s?=", base64.StdEncoding.EncodeToString([]byte(subject)))
mail := []byte(fmt.Sprintf("To: %s\r\n"+
"From: %s<%s>\r\n"+
"Subject: %s\r\n"+
"Content-Type: text/html; charset=UTF-8\r\n\r\n%s\r\n",
receiver, common.SystemName, common.SMTPAccount, encodedSubject, content))
auth := smtp.PlainAuth("", common.SMTPAccount, common.SMTPToken, common.SMTPServer)
addr := fmt.Sprintf("%s:%d", common.SMTPServer, common.SMTPPort)
receiver, config.SystemName, config.Account, encodedSubject, content))
auth := smtp.PlainAuth("", config.Account, config.Token, config.Server)
addr := fmt.Sprintf("%s:%d", config.Server, config.Port)
to := strings.Split(receiver, ";")
var err error
if common.SMTPPort == 465 {
if config.Port == 465 {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: common.SMTPServer,
ServerName: config.Server,
}
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", common.SMTPServer, common.SMTPPort), tlsConfig)
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", config.Server, config.Port), tlsConfig)
if err != nil {
return err
}
client, err := smtp.NewClient(conn, common.SMTPServer)
client, err := smtp.NewClient(conn, config.Server)
if err != nil {
return err
}
@@ -37,12 +46,12 @@ func SendEmail(subject string, receiver string, content string) error {
if err = client.Auth(auth); err != nil {
return err
}
if err = client.Mail(common.SMTPAccount); err != nil {
if err = client.Mail(config.Account); err != nil {
return err
}
receiverEmails := strings.Split(receiver, ";")
for _, receiver := range receiverEmails {
if err = client.Rcpt(receiver); err != nil {
for _, r := range receiverEmails {
if err = client.Rcpt(r); err != nil {
return err
}
}
@@ -59,7 +68,7 @@ func SendEmail(subject string, receiver string, content string) error {
return err
}
} else {
err = smtp.SendMail(addr, auth, common.SMTPAccount, to, mail)
err = smtp.SendMail(addr, auth, config.Account, to, mail)
}
return err
}