You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.4 KiB

  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 mailer
  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/log"
  11. "github.com/gogits/gogs/modules/middleware"
  12. )
  13. // Create New mail message use MailFrom and MailUser
  14. func NewMailMessage(To []string, subject, body string) Message {
  15. msg := NewHtmlMessage(To, base.MailService.User, subject, body)
  16. msg.User = base.MailService.User
  17. return msg
  18. }
  19. func GetMailTmplData(user *models.User) map[interface{}]interface{} {
  20. data := make(map[interface{}]interface{}, 10)
  21. data["AppName"] = base.AppName
  22. data["AppVer"] = base.AppVer
  23. data["AppUrl"] = base.AppUrl
  24. data["AppLogo"] = base.AppLogo
  25. data["ActiveCodeLives"] = base.Service.ActiveCodeLives / 60
  26. data["ResetPwdCodeLives"] = base.Service.ResetPwdCodeLives / 60
  27. if user != nil {
  28. data["User"] = user
  29. }
  30. return data
  31. }
  32. // create a time limit code for user active
  33. func CreateUserActiveCode(user *models.User, startInf interface{}) string {
  34. minutes := base.Service.ActiveCodeLives
  35. data := base.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands
  36. code := base.CreateTimeLimitCode(data, minutes, startInf)
  37. // add tail hex username
  38. code += hex.EncodeToString([]byte(user.LowerName))
  39. return code
  40. }
  41. // Send user register mail with active code
  42. func SendRegisterMail(r *middleware.Render, user *models.User) {
  43. code := CreateUserActiveCode(user, nil)
  44. subject := "Register success, Welcome"
  45. data := GetMailTmplData(user)
  46. data["Code"] = code
  47. body, err := r.HTMLString("mail/auth/register_success", data)
  48. if err != nil {
  49. log.Error("mail.SendRegisterMail(fail to render): %v", err)
  50. return
  51. }
  52. msg := NewMailMessage([]string{user.Email}, subject, body)
  53. msg.Info = fmt.Sprintf("UID: %d, send register mail", user.Id)
  54. SendAsync(&msg)
  55. }
  56. // Send email verify active email.
  57. func SendActiveMail(r *middleware.Render, user *models.User) {
  58. code := CreateUserActiveCode(user, nil)
  59. subject := "Verify your e-mail address"
  60. data := GetMailTmplData(user)
  61. data["Code"] = code
  62. body, err := r.HTMLString("mail/auth/active_email", data)
  63. if err != nil {
  64. log.Error("mail.SendActiveMail(fail to render): %v", err)
  65. return
  66. }
  67. msg := NewMailMessage([]string{user.Email}, subject, body)
  68. msg.Info = fmt.Sprintf("UID: %d, send email verify mail", user.Id)
  69. SendAsync(&msg)
  70. }