fusenapi/server/auth/internal/logic/email_manager.go

175 lines
4.1 KiB
Go
Raw Normal View History

2023-07-24 09:22:06 +00:00
package logic
import (
"bytes"
2023-08-31 03:12:49 +00:00
"fusenapi/utils/check"
2023-07-24 09:22:06 +00:00
"log"
"net/smtp"
"sync"
"text/template"
"time"
2023-08-31 04:20:05 +00:00
"github.com/zeromicro/go-zero/core/logx"
2023-07-24 09:22:06 +00:00
)
2023-08-31 04:20:05 +00:00
var EmailTaskResendTime = time.Second * 30
2023-08-31 03:12:49 +00:00
var TimeLimit *check.TimeLimit[string]
2023-07-24 09:22:06 +00:00
var EmailManager *EmailSender
2023-09-04 03:32:17 +00:00
var tpls *template.Template
2023-09-04 02:59:17 +00:00
2023-08-31 03:12:49 +00:00
func init() {
2023-09-04 03:32:17 +00:00
var err error
2023-08-31 03:12:49 +00:00
2023-09-04 03:32:17 +00:00
tpls, err = template.ParseGlob("../../../../html_template/*.tpl")
2023-09-04 02:59:17 +00:00
if err != nil {
log.Fatal(err)
}
2023-08-31 04:20:05 +00:00
TimeLimit = check.NewTimelimit[string](EmailTaskResendTime)
2023-08-31 03:12:49 +00:00
// Initialize the email manager
EmailManager = &EmailSender{
EmailTasks: make(chan *EmailFormat, 10),
Auth: smtp.PlainAuth(
"",
"support@fusenpack.com",
"wfbjpdgvaozjvwah",
"smtp.gmail.com",
),
FromEmail: "support@fusenpack.com",
emailSending: make(map[string]*EmailTask, 10),
2023-08-31 04:20:05 +00:00
ResendTimeLimit: EmailTaskResendTime,
semaphore: make(chan struct{}, 100), // Initialize semaphore with a capacity of 10
2023-08-31 03:12:49 +00:00
}
// Start processing email tasks
go EmailManager.ProcessEmailTasks()
// Start clearing expired tasks
go EmailManager.ClearExpiredTasks()
}
2023-07-27 08:48:43 +00:00
type EmailFormat struct {
2023-08-31 04:20:05 +00:00
UniqueKey string // 用于处理唯一的任务,重发都会被利用到
2023-07-27 08:48:43 +00:00
TargetEmail string // 发送的目标email
CompanyName string // fs公司名
ConfirmationLink string // fs确认连接
SenderName string // 发送人
SenderTitle string // 发送标题
}
2023-07-24 09:22:06 +00:00
// EmailSender
type EmailSender struct {
lock sync.Mutex
2023-07-27 08:48:43 +00:00
EmailTasks chan *EmailFormat
2023-07-24 11:43:56 +00:00
Auth smtp.Auth
FromEmail string
ResendTimeLimit time.Duration
2023-07-27 08:48:43 +00:00
emailSending map[string]*EmailTask
semaphore chan struct{}
2023-07-24 09:22:06 +00:00
}
// EmailTask
type EmailTask struct {
2023-07-27 08:48:43 +00:00
Email *EmailFormat // email
SendTime time.Time // 处理的任务时间
2023-07-24 09:22:06 +00:00
}
func (m *EmailSender) ProcessEmailTasks() {
for {
2023-07-27 08:48:43 +00:00
emailformat, ok := <-m.EmailTasks
2023-07-24 09:22:06 +00:00
if !ok {
log.Println("Email task channel closed")
break
}
2023-08-31 04:20:05 +00:00
if emailformat.UniqueKey == "" {
logx.Error("email UniqueKey must be exists")
continue
}
2023-07-24 09:22:06 +00:00
m.lock.Lock()
2023-08-31 04:20:05 +00:00
_, isSending := m.emailSending[emailformat.UniqueKey]
2023-07-24 09:22:06 +00:00
if isSending {
m.lock.Unlock()
continue
}
2023-08-31 04:20:05 +00:00
m.emailSending[emailformat.UniqueKey] = &EmailTask{
2023-07-27 08:48:43 +00:00
Email: emailformat,
2023-08-24 10:28:01 +00:00
SendTime: time.Now().UTC(),
2023-07-24 09:22:06 +00:00
}
m.lock.Unlock()
2023-07-24 11:43:56 +00:00
// Acquire a token
m.semaphore <- struct{}{}
go func() {
defer func() { <-m.semaphore }() // Release a token
2023-07-27 08:48:43 +00:00
content := RenderEmailTemplate(emailformat.CompanyName, emailformat.ConfirmationLink, emailformat.SenderName, emailformat.SenderTitle)
err := smtp.SendMail("smtp.gmail.com:587", m.Auth, m.FromEmail, []string{emailformat.TargetEmail}, content)
2023-07-24 11:43:56 +00:00
if err != nil {
2023-07-27 08:48:43 +00:00
log.Printf("Failed to send email to %s: %v\n", emailformat, err)
2023-08-31 04:20:05 +00:00
m.Resend(emailformat.UniqueKey, content)
2023-07-24 11:43:56 +00:00
}
}()
2023-07-24 09:22:06 +00:00
}
}
// Resend 重发邮件
2023-08-31 04:20:05 +00:00
func (m *EmailSender) Resend(uniqueKey string, content []byte) {
2023-07-24 09:22:06 +00:00
time.Sleep(m.ResendTimeLimit)
m.lock.Lock()
defer m.lock.Unlock()
// Check if the email task still exists and has not been sent successfully
2023-08-31 04:20:05 +00:00
if task, ok := m.emailSending[uniqueKey]; ok && task.SendTime.Add(m.ResendTimeLimit).After(time.Now().UTC()) {
err := smtp.SendMail(task.Email.TargetEmail, m.Auth, m.FromEmail, []string{task.Email.TargetEmail}, content)
2023-07-24 09:22:06 +00:00
if err != nil {
2023-08-31 04:20:05 +00:00
log.Printf("Failed to resend email to %s: %v\n", task.Email.TargetEmail, err)
2023-07-24 09:22:06 +00:00
} else {
2023-08-31 04:20:05 +00:00
delete(m.emailSending, uniqueKey)
2023-07-24 09:22:06 +00:00
}
}
}
// ClearExpiredTasks 清除过期的邮件任务
func (m *EmailSender) ClearExpiredTasks() {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
<-ticker.C
m.lock.Lock()
for email, task := range m.emailSending {
2023-08-24 10:28:01 +00:00
if task.SendTime.Add(m.ResendTimeLimit).Before(time.Now().UTC()) {
2023-07-24 09:22:06 +00:00
delete(m.emailSending, email)
}
}
m.lock.Unlock()
}
}
2023-07-24 11:43:56 +00:00
func RenderEmailTemplate(companyName, confirmationLink, senderName, senderTitle string) []byte {
2023-09-04 02:50:21 +00:00
2023-07-24 09:22:06 +00:00
data := map[string]string{
"CompanyName": companyName,
"ConfirmationLink": confirmationLink,
"SenderName": senderName,
"SenderTitle": senderTitle,
}
var result bytes.Buffer
2023-09-04 03:32:17 +00:00
err := tpls.ExecuteTemplate(&result, "email_register.tpl", data)
2023-07-24 09:22:06 +00:00
if err != nil {
log.Fatal(err)
}
2023-07-24 11:43:56 +00:00
return result.Bytes()
2023-07-24 09:22:06 +00:00
}