mail.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "github.com/Unknwon/macaron"
  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_RESET_PASSWORD base.TplName = "mail/auth/reset_passwd"
  18. AUTH_REGISTER_SUCCESS base.TplName = "mail/auth/register_success"
  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. // SendActivateAccountMail sends confirmation e-mail.
  54. func SendActivateEmailMail(c *macaron.Context, u *models.User, email *models.EmailAddress) {
  55. data := ComposeTplData(u)
  56. data["Code"] = u.GenerateEmailActivateCode(email.Email)
  57. data["Email"] = email.Email
  58. body, err := c.HTMLString(string(AUTH_ACTIVATE_EMAIL), data)
  59. if err != nil {
  60. log.Error(4, "HTMLString: %v", err)
  61. return
  62. }
  63. msg := NewMessage([]string{email.Email}, c.Tr("mail.activate_email"), body)
  64. msg.Info = fmt.Sprintf("UID: %d, activate email", u.Id)
  65. SendAsync(msg)
  66. }
  67. // SendIssueNotifyMail sends mail notification of all watchers of repository.
  68. func SendIssueNotifyMail(u, owner *models.User, repo *models.Repository, issue *models.Issue) ([]string, error) {
  69. ws, err := models.GetWatchers(repo.ID)
  70. if err != nil {
  71. return nil, fmt.Errorf("GetWatchers[%d]: %v", repo.ID, err)
  72. }
  73. tos := make([]string, 0, len(ws))
  74. for i := range ws {
  75. uid := ws[i].UserID
  76. if u.Id == uid {
  77. continue
  78. }
  79. to, err := models.GetUserByID(uid)
  80. if err != nil {
  81. return nil, fmt.Errorf("GetUserByID: %v", err)
  82. }
  83. if to.IsOrganization() {
  84. continue
  85. }
  86. tos = append(tos, to.Email)
  87. }
  88. if len(tos) == 0 {
  89. return tos, nil
  90. }
  91. subject := fmt.Sprintf("[%s] %s (#%d)", repo.Name, issue.Name, issue.Index)
  92. content := fmt.Sprintf("%s<br>-<br> <a href=\"%s%s/%s/issues/%d\">View it on Gogs</a>.",
  93. base.RenderSpecialLink([]byte(issue.Content), owner.Name+"/"+repo.Name),
  94. setting.AppUrl, owner.Name, repo.Name, issue.Index)
  95. msg := NewMessage(tos, subject, content)
  96. msg.Info = fmt.Sprintf("Subject: %s, issue notify", subject)
  97. SendAsync(msg)
  98. return tos, nil
  99. }
  100. // SendIssueMentionMail sends mail notification for who are mentioned in issue.
  101. func SendIssueMentionMail(r macaron.Render, u, owner *models.User,
  102. repo *models.Repository, issue *models.Issue, tos []string) error {
  103. if len(tos) == 0 {
  104. return nil
  105. }
  106. subject := fmt.Sprintf("[%s] %s (#%d)", repo.Name, issue.Name, issue.Index)
  107. data := ComposeTplData(nil)
  108. data["IssueLink"] = fmt.Sprintf("%s/%s/issues/%d", owner.Name, repo.Name, issue.Index)
  109. data["Subject"] = subject
  110. data["ActUserName"] = u.DisplayName()
  111. data["Content"] = string(base.RenderSpecialLink([]byte(issue.Content), owner.Name+"/"+repo.Name))
  112. body, err := r.HTMLString(string(NOTIFY_MENTION), data)
  113. if err != nil {
  114. return fmt.Errorf("HTMLString: %v", err)
  115. }
  116. msg := NewMessage(tos, subject, body)
  117. msg.Info = fmt.Sprintf("Subject: %s, issue mention", subject)
  118. SendAsync(msg)
  119. return nil
  120. }
  121. // SendCollaboratorMail sends mail notification to new collaborator.
  122. func SendCollaboratorMail(r macaron.Render, u, doer *models.User, repo *models.Repository) error {
  123. subject := fmt.Sprintf("%s added you to %s/%s", doer.Name, repo.Owner.Name, repo.Name)
  124. data := ComposeTplData(nil)
  125. data["RepoLink"] = path.Join(repo.Owner.Name, repo.Name)
  126. data["Subject"] = subject
  127. body, err := r.HTMLString(string(NOTIFY_COLLABORATOR), data)
  128. if err != nil {
  129. return fmt.Errorf("HTMLString: %v", err)
  130. }
  131. msg := NewMessage([]string{u.Email}, subject, body)
  132. msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.Id)
  133. SendAsync(msg)
  134. return nil
  135. }