mail.go 5.4 KB

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