mail.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "errors"
  8. "fmt"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/log"
  12. "github.com/gogits/gogs/modules/middleware"
  13. )
  14. // Create New mail message use MailFrom and MailUser
  15. func NewMailMessageFrom(To []string, from, subject, body string) Message {
  16. msg := NewHtmlMessage(To, from, subject, body)
  17. msg.User = base.MailService.User
  18. return msg
  19. }
  20. // Create New mail message use MailFrom and MailUser
  21. func NewMailMessage(To []string, subject, body string) Message {
  22. return NewMailMessageFrom(To, base.MailService.User, subject, body)
  23. }
  24. func GetMailTmplData(user *models.User) map[interface{}]interface{} {
  25. data := make(map[interface{}]interface{}, 10)
  26. data["AppName"] = base.AppName
  27. data["AppVer"] = base.AppVer
  28. data["AppUrl"] = base.AppUrl
  29. data["AppLogo"] = base.AppLogo
  30. data["ActiveCodeLives"] = base.Service.ActiveCodeLives / 60
  31. data["ResetPwdCodeLives"] = base.Service.ResetPwdCodeLives / 60
  32. if user != nil {
  33. data["User"] = user
  34. }
  35. return data
  36. }
  37. // create a time limit code for user active
  38. func CreateUserActiveCode(user *models.User, startInf interface{}) string {
  39. minutes := base.Service.ActiveCodeLives
  40. data := base.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands
  41. code := base.CreateTimeLimitCode(data, minutes, startInf)
  42. // add tail hex username
  43. code += hex.EncodeToString([]byte(user.LowerName))
  44. return code
  45. }
  46. // Send user register mail with active code
  47. func SendRegisterMail(r *middleware.Render, user *models.User) {
  48. code := CreateUserActiveCode(user, nil)
  49. subject := "Register success, Welcome"
  50. data := GetMailTmplData(user)
  51. data["Code"] = code
  52. body, err := r.HTMLString("mail/auth/register_success", data)
  53. if err != nil {
  54. log.Error("mail.SendRegisterMail(fail to render): %v", err)
  55. return
  56. }
  57. msg := NewMailMessage([]string{user.Email}, subject, body)
  58. msg.Info = fmt.Sprintf("UID: %d, send register mail", user.Id)
  59. SendAsync(&msg)
  60. }
  61. // Send email verify active email.
  62. func SendActiveMail(r *middleware.Render, user *models.User) {
  63. code := CreateUserActiveCode(user, nil)
  64. subject := "Verify your e-mail address"
  65. data := GetMailTmplData(user)
  66. data["Code"] = code
  67. body, err := r.HTMLString("mail/auth/active_email", data)
  68. if err != nil {
  69. log.Error("mail.SendActiveMail(fail to render): %v", err)
  70. return
  71. }
  72. msg := NewMailMessage([]string{user.Email}, subject, body)
  73. msg.Info = fmt.Sprintf("UID: %d, send email verify mail", user.Id)
  74. SendAsync(&msg)
  75. }
  76. // SendNotifyMail sends mail notification of all watchers.
  77. func SendNotifyMail(userId, repoId int64, userName, repoName, subject, content string) error {
  78. watches, err := models.GetWatches(repoId)
  79. if err != nil {
  80. return errors.New("mail.NotifyWatchers(get watches): " + err.Error())
  81. }
  82. tos := make([]string, 0, len(watches))
  83. for i := range watches {
  84. uid := watches[i].UserId
  85. if userId == uid {
  86. continue
  87. }
  88. u, err := models.GetUserById(uid)
  89. if err != nil {
  90. return errors.New("mail.NotifyWatchers(get user): " + err.Error())
  91. }
  92. tos = append(tos, u.Email)
  93. }
  94. if len(tos) == 0 {
  95. return nil
  96. }
  97. msg := NewMailMessageFrom(tos, userName, subject, content)
  98. msg.Info = fmt.Sprintf("Subject: %s, send notify emails", subject)
  99. SendAsync(&msg)
  100. return nil
  101. }