mail.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package auth
  5. import (
  6. "encoding/hex"
  7. "fmt"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/mailer"
  11. )
  12. // create a time limit code for user active
  13. func CreateUserActiveCode(user *models.User, startInf interface{}) string {
  14. hours := base.Service.ActiveCodeLives / 60
  15. data := base.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands
  16. code := base.CreateTimeLimitCode(data, hours, startInf)
  17. // add tail hex username
  18. code += hex.EncodeToString([]byte(user.LowerName))
  19. return code
  20. }
  21. // Send user register mail with active code
  22. func SendRegisterMail(user *models.User) {
  23. code := CreateUserActiveCode(user, nil)
  24. subject := "Register success, Welcome"
  25. data := mailer.GetMailTmplData(user)
  26. data["Code"] = code
  27. body := base.RenderTemplate("mail/auth/register_success.html", data)
  28. msg := mailer.NewMailMessage([]string{user.Email}, subject, body)
  29. msg.Info = fmt.Sprintf("UID: %d, send register mail", user.Id)
  30. // async send mail
  31. mailer.SendAsync(msg)
  32. }
  33. // Send email verify active email.
  34. func SendActiveMail(user *models.User) {
  35. code := CreateUserActiveCode(user, nil)
  36. subject := "Verify your email address"
  37. data := mailer.GetMailTmplData(user)
  38. data["Code"] = code
  39. body := base.RenderTemplate("mail/auth/active_email.html", data)
  40. msg := mailer.NewMailMessage([]string{user.Email}, subject, body)
  41. msg.Info = fmt.Sprintf("UID: %d, send email verify mail", user.Id)
  42. // async send mail
  43. mailer.SendAsync(msg)
  44. }