mail.go 5.6 KB

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