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.

190 lines
5.7 KiB

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. "fmt"
  7. "path"
  8. "strings"
  9. "gopkg.in/gomail.v2"
  10. "gopkg.in/macaron.v1"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/log"
  14. "github.com/gogits/gogs/modules/markdown"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. const (
  18. AUTH_ACTIVATE base.TplName = "mail/auth/activate"
  19. AUTH_ACTIVATE_EMAIL base.TplName = "mail/auth/activate_email"
  20. AUTH_REGISTER_NOTIFY base.TplName = "mail/auth/register_notify"
  21. AUTH_RESET_PASSWORD base.TplName = "mail/auth/reset_passwd"
  22. NOTIFY_COLLABORATOR base.TplName = "mail/notify/collaborator"
  23. NOTIFY_MENTION base.TplName = "mail/notify/mention"
  24. )
  25. func ComposeTplData(u *models.User) map[interface{}]interface{} {
  26. data := make(map[interface{}]interface{}, 10)
  27. data["AppName"] = setting.AppName
  28. data["AppVer"] = setting.AppVer
  29. data["AppUrl"] = setting.AppUrl
  30. data["ActiveCodeLives"] = setting.Service.ActiveCodeLives / 60
  31. data["ResetPwdCodeLives"] = setting.Service.ResetPwdCodeLives / 60
  32. if u != nil {
  33. data["User"] = u
  34. }
  35. return data
  36. }
  37. func SendUserMail(c *macaron.Context, u *models.User, tpl base.TplName, code, subject, info string) {
  38. data := ComposeTplData(u)
  39. data["Code"] = code
  40. body, err := c.HTMLString(string(tpl), data)
  41. if err != nil {
  42. log.Error(4, "HTMLString: %v", err)
  43. return
  44. }
  45. msg := NewMessage([]string{u.Email}, subject, body)
  46. msg.Info = fmt.Sprintf("UID: %d, %s", u.Id, info)
  47. SendAsync(msg)
  48. }
  49. func SendActivateAccountMail(c *macaron.Context, u *models.User) {
  50. SendUserMail(c, u, AUTH_ACTIVATE, u.GenerateActivateCode(), c.Tr("mail.activate_account"), "activate account")
  51. }
  52. // SendResetPasswordMail sends reset password e-mail.
  53. func SendResetPasswordMail(c *macaron.Context, u *models.User) {
  54. SendUserMail(c, u, AUTH_RESET_PASSWORD, u.GenerateActivateCode(), c.Tr("mail.reset_password"), "reset password")
  55. }
  56. // SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
  57. func SendRegisterNotifyMail(c *macaron.Context, u *models.User) {
  58. body, err := c.HTMLString(string(AUTH_REGISTER_NOTIFY), ComposeTplData(u))
  59. if err != nil {
  60. log.Error(4, "HTMLString: %v", err)
  61. return
  62. }
  63. msg := NewMessage([]string{u.Email}, c.Tr("mail.register_notify"), body)
  64. msg.Info = fmt.Sprintf("UID: %d, registration notify", u.Id)
  65. SendAsync(msg)
  66. }
  67. // SendActivateAccountMail sends confirmation e-mail.
  68. func SendActivateEmailMail(c *macaron.Context, u *models.User, email *models.EmailAddress) {
  69. data := ComposeTplData(u)
  70. data["Code"] = u.GenerateEmailActivateCode(email.Email)
  71. data["Email"] = email.Email
  72. body, err := c.HTMLString(string(AUTH_ACTIVATE_EMAIL), data)
  73. if err != nil {
  74. log.Error(4, "HTMLString: %v", err)
  75. return
  76. }
  77. msg := NewMessage([]string{email.Email}, c.Tr("mail.activate_email"), body)
  78. msg.Info = fmt.Sprintf("UID: %d, activate email", u.Id)
  79. SendAsync(msg)
  80. }
  81. // SendIssueNotifyMail sends mail notification of all watchers of repository.
  82. func SendIssueNotifyMail(u, owner *models.User, repo *models.Repository, issue *models.Issue) ([]string, error) {
  83. ws, err := models.GetWatchers(repo.ID)
  84. if err != nil {
  85. return nil, fmt.Errorf("GetWatchers[%d]: %v", repo.ID, err)
  86. }
  87. tos := make([]string, 0, len(ws))
  88. for i := range ws {
  89. uid := ws[i].UserID
  90. if u.Id == uid {
  91. continue
  92. }
  93. to, err := models.GetUserByID(uid)
  94. if err != nil {
  95. return nil, fmt.Errorf("GetUserByID: %v", err)
  96. }
  97. if to.IsOrganization() {
  98. continue
  99. }
  100. tos = append(tos, to.Email)
  101. }
  102. if len(tos) == 0 {
  103. return tos, nil
  104. }
  105. subject := fmt.Sprintf("[%s] %s (#%d)", repo.Name, issue.Name, issue.Index)
  106. content := fmt.Sprintf("%s<br>-<br> <a href=\"%s%s/%s/issues/%d\">View it on Gogs</a>.",
  107. markdown.RenderSpecialLink([]byte(strings.Replace(issue.Content, "\n", "<br>", -1)), owner.Name+"/"+repo.Name, repo.ComposeMetas()),
  108. setting.AppUrl, owner.Name, repo.Name, issue.Index)
  109. msg := NewMessage(tos, subject, content)
  110. msg.Info = fmt.Sprintf("Subject: %s, issue notify", subject)
  111. SendAsync(msg)
  112. return tos, nil
  113. }
  114. // SendIssueMentionMail sends mail notification for who are mentioned in issue.
  115. func SendIssueMentionMail(r macaron.Render, u, owner *models.User,
  116. repo *models.Repository, issue *models.Issue, tos []string) error {
  117. if len(tos) == 0 {
  118. return nil
  119. }
  120. subject := fmt.Sprintf("[%s] %s (#%d)", repo.Name, issue.Name, issue.Index)
  121. data := ComposeTplData(nil)
  122. data["IssueLink"] = fmt.Sprintf("%s/%s/issues/%d", owner.Name, repo.Name, issue.Index)
  123. data["Subject"] = subject
  124. data["ActUserName"] = u.DisplayName()
  125. data["Content"] = string(markdown.RenderSpecialLink([]byte(issue.Content), owner.Name+"/"+repo.Name, repo.ComposeMetas()))
  126. body, err := r.HTMLString(string(NOTIFY_MENTION), data)
  127. if err != nil {
  128. return fmt.Errorf("HTMLString: %v", err)
  129. }
  130. msg := NewMessage(tos, subject, body)
  131. msg.Info = fmt.Sprintf("Subject: %s, issue mention", subject)
  132. SendAsync(msg)
  133. return nil
  134. }
  135. // SendCollaboratorMail sends mail notification to new collaborator.
  136. func SendCollaboratorMail(r macaron.Render, u, doer *models.User, repo *models.Repository) error {
  137. subject := fmt.Sprintf("%s added you to %s/%s", doer.Name, repo.Owner.Name, repo.Name)
  138. data := ComposeTplData(nil)
  139. data["RepoLink"] = path.Join(repo.Owner.Name, repo.Name)
  140. data["Subject"] = subject
  141. body, err := r.HTMLString(string(NOTIFY_COLLABORATOR), data)
  142. if err != nil {
  143. return fmt.Errorf("HTMLString: %v", err)
  144. }
  145. msg := NewMessage([]string{u.Email}, subject, body)
  146. msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.Id)
  147. SendAsync(msg)
  148. return nil
  149. }
  150. func SendTestMail(email string) error {
  151. return gomail.Send(&Sender{}, NewMessage([]string{email}, "Gogs Test Email!", "Gogs Test Email!").Message)
  152. }