diff options
Diffstat (limited to 'modules/mailer')
-rw-r--r-- | modules/mailer/mail.go | 14 | ||||
-rw-r--r-- | modules/mailer/mailer.go | 44 |
2 files changed, 35 insertions, 23 deletions
diff --git a/modules/mailer/mail.go b/modules/mailer/mail.go index de4f24a4..92acd20e 100644 --- a/modules/mailer/mail.go +++ b/modules/mailer/mail.go @@ -37,9 +37,9 @@ func GetMailTmplData(user *models.User) map[interface{}]interface{} { // create a time limit code for user active func CreateUserActiveCode(user *models.User, startInf interface{}) string { - hours := base.Service.ActiveCodeLives / 60 + minutes := base.Service.ActiveCodeLives data := base.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands - code := base.CreateTimeLimitCode(data, hours, startInf) + code := base.CreateTimeLimitCode(data, minutes, startInf) // add tail hex username code += hex.EncodeToString([]byte(user.LowerName)) @@ -62,19 +62,18 @@ func SendRegisterMail(r *middleware.Render, user *models.User) { msg := NewMailMessage([]string{user.Email}, subject, body) msg.Info = fmt.Sprintf("UID: %d, send register mail", user.Id) - // async send mail - SendAsync(msg) + SendAsync(&msg) } // Send email verify active email. func SendActiveMail(r *middleware.Render, user *models.User) { code := CreateUserActiveCode(user, nil) - subject := "Verify your email address" + subject := "Verify your e-mail address" data := GetMailTmplData(user) data["Code"] = code - body, err := r.HTMLString("mail/auth/active_email.html", data) + body, err := r.HTMLString("mail/auth/active_email", data) if err != nil { log.Error("mail.SendActiveMail(fail to render): %v", err) return @@ -83,6 +82,5 @@ func SendActiveMail(r *middleware.Render, user *models.User) { msg := NewMailMessage([]string{user.Email}, subject, body) msg.Info = fmt.Sprintf("UID: %d, send email verify mail", user.Id) - // async send mail - SendAsync(msg) + SendAsync(&msg) } diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go index cc76acb2..3823e01f 100644 --- a/modules/mailer/mailer.go +++ b/modules/mailer/mailer.go @@ -38,8 +38,34 @@ func (m Message) Content() string { return content } +var mailQueue chan *Message + +func init() { + mailQueue = make(chan *Message, base.Cfg.MustInt("mailer", "SEND_BUFFER_LEN", 10)) + go processMailQueue() +} + +func processMailQueue() { + for { + select { + case msg := <-mailQueue: + num, err := Send(msg) + tos := strings.Join(msg.To, "; ") + info := "" + if err != nil { + if len(msg.Info) > 0 { + info = ", info: " + msg.Info + } + log.Error(fmt.Sprintf("Async sent email %d succeed, not send emails: %s%s err: %s", num, tos, info, err)) + return + } + log.Trace(fmt.Sprintf("Async sent email %d succeed, sent emails: %s%s", num, tos, info)) + } + } +} + // Direct Send mail message -func Send(msg Message) (int, error) { +func Send(msg *Message) (int, error) { log.Trace("Sending mails to: %s", strings.Join(msg.To, "; ")) host := strings.Split(base.MailService.Host, ":") @@ -82,21 +108,9 @@ func Send(msg Message) (int, error) { } // Async Send mail message -func SendAsync(msg Message) { - // TODO may be need pools limit concurrent nums +func SendAsync(msg *Message) { go func() { - num, err := Send(msg) - tos := strings.Join(msg.To, "; ") - info := "" - if err != nil { - if len(msg.Info) > 0 { - info = ", info: " + msg.Info - } - // log failed - log.Error(fmt.Sprintf("Async sent email %d succeed, not send emails: %s%s err: %s", num, tos, info, err)) - return - } - log.Trace(fmt.Sprintf("Async sent email %d succeed, sent emails: %s%s", num, tos, info)) + mailQueue <- msg }() } |