mail.go 5.0 KB

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