Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

mail.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 active mail", user.Id)
  74. SendAsync(&msg)
  75. }
  76. // Send reset password email.
  77. func SendResetPasswdMail(r *middleware.Render, user *models.User) {
  78. code := CreateUserActiveCode(user, nil)
  79. subject := "Reset your password"
  80. data := GetMailTmplData(user)
  81. data["Code"] = code
  82. body, err := r.HTMLString("mail/auth/reset_passwd", data)
  83. if err != nil {
  84. log.Error("mail.SendResetPasswdMail(fail to render): %v", err)
  85. return
  86. }
  87. msg := NewMailMessage([]string{user.Email}, subject, body)
  88. msg.Info = fmt.Sprintf("UID: %d, send reset password email", user.Id)
  89. SendAsync(&msg)
  90. }
  91. // SendIssueNotifyMail sends mail notification of all watchers of repository.
  92. func SendIssueNotifyMail(user, owner *models.User, repo *models.Repository, issue *models.Issue) ([]string, error) {
  93. watches, err := models.GetWatches(repo.Id)
  94. if err != nil {
  95. return nil, errors.New("mail.NotifyWatchers(get watches): " + err.Error())
  96. }
  97. tos := make([]string, 0, len(watches))
  98. for i := range watches {
  99. uid := watches[i].UserId
  100. if user.Id == uid {
  101. continue
  102. }
  103. u, err := models.GetUserById(uid)
  104. if err != nil {
  105. return nil, errors.New("mail.NotifyWatchers(get user): " + err.Error())
  106. }
  107. tos = append(tos, u.Email)
  108. }
  109. if len(tos) == 0 {
  110. return tos, nil
  111. }
  112. subject := fmt.Sprintf("[%s] %s", repo.Name, issue.Name)
  113. content := fmt.Sprintf("%s<br>-<br> <a href=\"%s%s/%s/issues/%d\">View it on Gogs</a>.",
  114. base.RenderSpecialLink([]byte(issue.Content), owner.Name+"/"+repo.Name),
  115. base.AppUrl, owner.Name, repo.Name, issue.Index)
  116. msg := NewMailMessageFrom(tos, user.Name, subject, content)
  117. msg.Info = fmt.Sprintf("Subject: %s, send issue notify emails", subject)
  118. SendAsync(&msg)
  119. return tos, nil
  120. }
  121. // SendIssueMentionMail sends mail notification for who are mentioned in issue.
  122. func SendIssueMentionMail(user, owner *models.User, repo *models.Repository, issue *models.Issue, tos []string) error {
  123. if len(tos) == 0 {
  124. return nil
  125. }
  126. issueLink := fmt.Sprintf("%s%s/%s/issues/%d", base.AppUrl, owner.Name, repo.Name, issue.Index)
  127. body := fmt.Sprintf(`%s mentioned you.`, user.Name)
  128. subject := fmt.Sprintf("[%s] %s", repo.Name, issue.Name)
  129. content := fmt.Sprintf("%s<br>-<br> <a href=\"%s\">View it on Gogs</a>.", body, issueLink)
  130. msg := NewMailMessageFrom(tos, user.Name, subject, content)
  131. msg.Info = fmt.Sprintf("Subject: %s, send issue mention emails", subject)
  132. SendAsync(&msg)
  133. return nil
  134. }