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.

205 lines
5.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. "path"
  10. "github.com/Unknwon/com"
  11. "github.com/Unknwon/macaron"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. const (
  18. AUTH_ACTIVE base.TplName = "mail/auth/active"
  19. AUTH_REGISTER_SUCCESS base.TplName = "mail/auth/register_success"
  20. AUTH_RESET_PASSWORD base.TplName = "mail/auth/reset_passwd"
  21. NOTIFY_COLLABORATOR base.TplName = "mail/notify/collaborator"
  22. NOTIFY_MENTION base.TplName = "mail/notify/mention"
  23. )
  24. // Create New mail message use MailFrom and MailUser
  25. func NewMailMessageFrom(To []string, from, subject, body string) Message {
  26. msg := NewHtmlMessage(To, from, subject, body)
  27. msg.User = setting.MailService.User
  28. return msg
  29. }
  30. // Create New mail message use MailFrom and MailUser
  31. func NewMailMessage(To []string, subject, body string) Message {
  32. return NewMailMessageFrom(To, setting.MailService.From, subject, body)
  33. }
  34. func GetMailTmplData(u *models.User) map[interface{}]interface{} {
  35. data := make(map[interface{}]interface{}, 10)
  36. data["AppName"] = setting.AppName
  37. data["AppVer"] = setting.AppVer
  38. data["AppUrl"] = setting.AppUrl
  39. data["ActiveCodeLives"] = setting.Service.ActiveCodeLives / 60
  40. data["ResetPwdCodeLives"] = setting.Service.ResetPwdCodeLives / 60
  41. if u != nil {
  42. data["User"] = u
  43. }
  44. return data
  45. }
  46. // create a time limit code for user active
  47. func CreateUserActiveCode(u *models.User, startInf interface{}) string {
  48. minutes := setting.Service.ActiveCodeLives
  49. data := com.ToStr(u.Id) + u.Email + u.LowerName + u.Passwd + u.Rands
  50. code := base.CreateTimeLimitCode(data, minutes, startInf)
  51. // add tail hex username
  52. code += hex.EncodeToString([]byte(u.LowerName))
  53. return code
  54. }
  55. // Send user register mail with active code
  56. func SendRegisterMail(r macaron.Render, u *models.User) {
  57. code := CreateUserActiveCode(u, nil)
  58. subject := "Register success, Welcome"
  59. data := GetMailTmplData(u)
  60. data["Code"] = code
  61. body, err := r.HTMLString(string(AUTH_REGISTER_SUCCESS), data)
  62. if err != nil {
  63. log.Error(4, "mail.SendRegisterMail(fail to render): %v", err)
  64. return
  65. }
  66. msg := NewMailMessage([]string{u.Email}, subject, body)
  67. msg.Info = fmt.Sprintf("UID: %d, send register mail", u.Id)
  68. SendAsync(&msg)
  69. }
  70. // Send email verify active email.
  71. func SendActiveMail(r macaron.Render, u *models.User) {
  72. code := CreateUserActiveCode(u, nil)
  73. subject := "Verify your e-mail address"
  74. data := GetMailTmplData(u)
  75. data["Code"] = code
  76. body, err := r.HTMLString(string(AUTH_ACTIVE), data)
  77. if err != nil {
  78. log.Error(4, "mail.SendActiveMail(fail to render): %v", err)
  79. return
  80. }
  81. msg := NewMailMessage([]string{u.Email}, subject, body)
  82. msg.Info = fmt.Sprintf("UID: %d, send active mail", u.Id)
  83. SendAsync(&msg)
  84. }
  85. // Send reset password email.
  86. func SendResetPasswdMail(r macaron.Render, u *models.User) {
  87. code := CreateUserActiveCode(u, nil)
  88. subject := "Reset your password"
  89. data := GetMailTmplData(u)
  90. data["Code"] = code
  91. body, err := r.HTMLString(string(AUTH_RESET_PASSWORD), data)
  92. if err != nil {
  93. log.Error(4, "mail.SendResetPasswdMail(fail to render): %v", err)
  94. return
  95. }
  96. msg := NewMailMessage([]string{u.Email}, subject, body)
  97. msg.Info = fmt.Sprintf("UID: %d, send reset password email", u.Id)
  98. SendAsync(&msg)
  99. }
  100. // SendIssueNotifyMail sends mail notification of all watchers of repository.
  101. func SendIssueNotifyMail(u, owner *models.User, repo *models.Repository, issue *models.Issue) ([]string, error) {
  102. ws, err := models.GetWatchers(repo.Id)
  103. if err != nil {
  104. return nil, errors.New("mail.NotifyWatchers(GetWatchers): " + err.Error())
  105. }
  106. tos := make([]string, 0, len(ws))
  107. for i := range ws {
  108. uid := ws[i].UserId
  109. if u.Id == uid {
  110. continue
  111. }
  112. u, err := models.GetUserById(uid)
  113. if err != nil {
  114. return nil, errors.New("mail.NotifyWatchers(GetUserById): " + err.Error())
  115. }
  116. tos = append(tos, u.Email)
  117. }
  118. if len(tos) == 0 {
  119. return tos, nil
  120. }
  121. subject := fmt.Sprintf("[%s] %s(#%d)", repo.Name, issue.Name, issue.Index)
  122. content := fmt.Sprintf("%s<br>-<br> <a href=\"%s%s/%s/issues/%d\">View it on Gogs</a>.",
  123. base.RenderSpecialLink([]byte(issue.Content), owner.Name+"/"+repo.Name),
  124. setting.AppUrl, owner.Name, repo.Name, issue.Index)
  125. msg := NewMailMessageFrom(tos, u.Email, subject, content)
  126. msg.Info = fmt.Sprintf("Subject: %s, send issue notify emails", subject)
  127. SendAsync(&msg)
  128. return tos, nil
  129. }
  130. // SendIssueMentionMail sends mail notification for who are mentioned in issue.
  131. func SendIssueMentionMail(r macaron.Render, u, owner *models.User,
  132. repo *models.Repository, issue *models.Issue, tos []string) error {
  133. if len(tos) == 0 {
  134. return nil
  135. }
  136. subject := fmt.Sprintf("[%s] %s(#%d)", repo.Name, issue.Name, issue.Index)
  137. data := GetMailTmplData(nil)
  138. data["IssueLink"] = fmt.Sprintf("%s/%s/issues/%d", owner.Name, repo.Name, issue.Index)
  139. data["Subject"] = subject
  140. body, err := r.HTMLString(string(NOTIFY_MENTION), data)
  141. if err != nil {
  142. return fmt.Errorf("mail.SendIssueMentionMail(fail to render): %v", err)
  143. }
  144. msg := NewMailMessageFrom(tos, u.Email, subject, body)
  145. msg.Info = fmt.Sprintf("Subject: %s, send issue mention emails", subject)
  146. SendAsync(&msg)
  147. return nil
  148. }
  149. // SendCollaboratorMail sends mail notification to new collaborator.
  150. func SendCollaboratorMail(r macaron.Render, u, owner *models.User,
  151. repo *models.Repository) error {
  152. subject := fmt.Sprintf("%s added you to %s", owner.Name, repo.Name)
  153. data := GetMailTmplData(nil)
  154. data["RepoLink"] = path.Join(owner.Name, repo.Name)
  155. data["Subject"] = subject
  156. body, err := r.HTMLString(string(NOTIFY_COLLABORATOR), data)
  157. if err != nil {
  158. return fmt.Errorf("mail.SendCollaboratorMail(fail to render): %v", err)
  159. }
  160. msg := NewMailMessage([]string{u.Email}, subject, body)
  161. msg.Info = fmt.Sprintf("UID: %d, send register mail", u.Id)
  162. SendAsync(&msg)
  163. return nil
  164. }