TODO: Email Valid Url
This commit is contained in:
parent
b43b5dbd59
commit
a248c6cbeb
|
@ -29,7 +29,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Method: http.MethodGet,
|
Method: http.MethodGet,
|
||||||
Path: "/api/auth/oauth2/login/register",
|
Path: "/api/auth/oauth2/register",
|
||||||
Handler: UserEmailRegisterHandler(serverCtx),
|
Handler: UserEmailRegisterHandler(serverCtx),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -14,11 +14,12 @@ var EmailManager *EmailSender
|
||||||
// EmailSender
|
// EmailSender
|
||||||
type EmailSender struct {
|
type EmailSender struct {
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
EmailTasks chan string // 处理email的队列
|
EmailTasks chan string
|
||||||
Auth smtp.Auth // 邮箱发送处理
|
Auth smtp.Auth
|
||||||
FromEmail string // 发送的email, 公司email
|
FromEmail string
|
||||||
emailSending map[string]*EmailTask // 正在发送的邮件
|
emailSending map[string]*EmailTask
|
||||||
ResendTimeLimit time.Duration // 重发时间限制
|
ResendTimeLimit time.Duration
|
||||||
|
semaphore chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// EmailTask
|
// EmailTask
|
||||||
|
@ -27,7 +28,6 @@ type EmailTask struct {
|
||||||
SendTime time.Time // 处理的任务时间
|
SendTime time.Time // 处理的任务时间
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProcessEmailTasks 处理邮件队列
|
|
||||||
func (m *EmailSender) ProcessEmailTasks() {
|
func (m *EmailSender) ProcessEmailTasks() {
|
||||||
for {
|
for {
|
||||||
emailTarget, ok := <-m.EmailTasks
|
emailTarget, ok := <-m.EmailTasks
|
||||||
|
@ -49,12 +49,19 @@ func (m *EmailSender) ProcessEmailTasks() {
|
||||||
}
|
}
|
||||||
m.lock.Unlock()
|
m.lock.Unlock()
|
||||||
|
|
||||||
content := []byte("Hello, this is a test email")
|
// Acquire a token
|
||||||
err := smtp.SendMail(emailTarget, m.Auth, m.FromEmail, []string{emailTarget}, content)
|
m.semaphore <- struct{}{}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer func() { <-m.semaphore }() // Release a token
|
||||||
|
|
||||||
|
content := RenderEmailTemplate("fusen", "http://www.baidu.com", "fusen@gmail.com", "mail-valid")
|
||||||
|
err := smtp.SendMail("smtp.gmail.com:587", m.Auth, m.FromEmail, []string{emailTarget}, content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to send email to %s: %v\n", emailTarget, err)
|
log.Printf("Failed to send email to %s: %v\n", emailTarget, err)
|
||||||
m.Resend(emailTarget, content)
|
m.Resend(emailTarget, content)
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,6 +115,7 @@ func init() {
|
||||||
FromEmail: "user@example.com",
|
FromEmail: "user@example.com",
|
||||||
emailSending: make(map[string]*EmailTask, 10),
|
emailSending: make(map[string]*EmailTask, 10),
|
||||||
ResendTimeLimit: time.Minute * 1,
|
ResendTimeLimit: time.Minute * 1,
|
||||||
|
semaphore: make(chan struct{}, 10), // Initialize semaphore with a capacity of 10
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start processing email tasks
|
// Start processing email tasks
|
||||||
|
@ -137,7 +145,7 @@ Thanks,
|
||||||
{{.CompanyName}}
|
{{.CompanyName}}
|
||||||
`
|
`
|
||||||
|
|
||||||
func RenderEmailTemplate(companyName, recipient, confirmationLink, senderName, senderTitle string) string {
|
func RenderEmailTemplate(companyName, confirmationLink, senderName, senderTitle string) []byte {
|
||||||
tmpl, err := template.New("email").Parse(emailTemplate)
|
tmpl, err := template.New("email").Parse(emailTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -156,5 +164,5 @@ func RenderEmailTemplate(companyName, recipient, confirmationLink, senderName, s
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.String()
|
return result.Bytes()
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,6 +158,8 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
|
||||||
return resp.SetStatus(basic.CodeOAuthEmailErr)
|
return resp.SetStatus(basic.CodeOAuthEmailErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EmailManager.EmailTasks <- req.Email // email进入队
|
||||||
|
|
||||||
return resp.SetStatus(basic.CodeOK)
|
return resp.SetStatus(basic.CodeOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ service auth {
|
||||||
get /api/auth/oauth2/login/google(RequestGoogleLogin) returns (response);
|
get /api/auth/oauth2/login/google(RequestGoogleLogin) returns (response);
|
||||||
|
|
||||||
@handler UserEmailRegisterHandler
|
@handler UserEmailRegisterHandler
|
||||||
get /api/auth/oauth2/login/register(RequestEmailRegister) returns (response);
|
get /api/auth/oauth2/register(RequestEmailRegister) returns (response);
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserAddAddressHandler 用户登录请求结构
|
// UserAddAddressHandler 用户登录请求结构
|
||||||
|
|
Loading…
Reference in New Issue
Block a user