You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mail.go 5.2 kB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_REGISTER_SUCCESS base.TplName = "mail/auth/register_success"
  17. AUTH_ACTIVATE base.TplName = "mail/auth/activate"
  18. AUTH_ACTIVATE_EMAIL base.TplName = "mail/auth/activate_email"
  19. AUTH_RESET_PASSWORD base.TplName = "mail/auth/reset_passwd"
  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: %s", u.Id, email.Email)
  59. SendAsync(msg)
  60. }
  61. // Send reset password email.
  62. func SendResetPasswdMail(r macaron.Render, u *models.User) {
  63. code := u.GenerateActivateCode()
  64. subject := "Reset your password"
  65. data := ComposeTplData(u)
  66. data["Code"] = code
  67. body, err := r.HTMLString(string(AUTH_RESET_PASSWORD), data)
  68. if err != nil {
  69. log.Error(4, "mail.SendResetPasswdMail(fail to render): %v", err)
  70. return
  71. }
  72. msg := NewMessage([]string{u.Email}, subject, body)
  73. msg.Info = fmt.Sprintf("UID: %d, send reset password email", u.Id)
  74. SendAsync(msg)
  75. }
  76. // SendIssueNotifyMail sends mail notification of all watchers of repository.
  77. func SendIssueNotifyMail(u, owner *models.User, repo *models.Repository, issue *models.Issue) ([]string, error) {
  78. ws, err := models.GetWatchers(repo.ID)
  79. if err != nil {
  80. return nil, errors.New("mail.NotifyWatchers(GetWatchers): " + err.Error())
  81. }
  82. tos := make([]string, 0, len(ws))
  83. for i := range ws {
  84. uid := ws[i].UserID
  85. if u.Id == uid {
  86. continue
  87. }
  88. u, err := models.GetUserByID(uid)
  89. if err != nil {
  90. return nil, errors.New("mail.NotifyWatchers(GetUserById): " + err.Error())
  91. }
  92. tos = append(tos, u.Email)
  93. }
  94. if len(tos) == 0 {
  95. return tos, nil
  96. }
  97. subject := fmt.Sprintf("[%s] %s (#%d)", repo.Name, issue.Name, issue.Index)
  98. content := fmt.Sprintf("%s<br>-<br> <a href=\"%s%s/%s/issues/%d\">View it on Gogs</a>.",
  99. base.RenderSpecialLink([]byte(issue.Content), owner.Name+"/"+repo.Name),
  100. setting.AppUrl, owner.Name, repo.Name, issue.Index)
  101. msg := NewMessageFrom(tos, u.Email, subject, content)
  102. msg.Info = fmt.Sprintf("Subject: %s, send issue notify emails", subject)
  103. SendAsync(msg)
  104. return tos, nil
  105. }
  106. // SendIssueMentionMail sends mail notification for who are mentioned in issue.
  107. func SendIssueMentionMail(r macaron.Render, u, owner *models.User,
  108. repo *models.Repository, issue *models.Issue, tos []string) error {
  109. if len(tos) == 0 {
  110. return nil
  111. }
  112. subject := fmt.Sprintf("[%s] %s (#%d)", repo.Name, issue.Name, issue.Index)
  113. data := ComposeTplData(nil)
  114. data["IssueLink"] = fmt.Sprintf("%s/%s/issues/%d", owner.Name, repo.Name, issue.Index)
  115. data["Subject"] = subject
  116. body, err := r.HTMLString(string(NOTIFY_MENTION), data)
  117. if err != nil {
  118. return fmt.Errorf("mail.SendIssueMentionMail(fail to render): %v", err)
  119. }
  120. msg := NewMessageFrom(tos, u.Email, subject, body)
  121. msg.Info = fmt.Sprintf("Subject: %s, send issue mention emails", subject)
  122. SendAsync(msg)
  123. return nil
  124. }
  125. // SendCollaboratorMail sends mail notification to new collaborator.
  126. func SendCollaboratorMail(r macaron.Render, u, owner *models.User,
  127. repo *models.Repository) error {
  128. subject := fmt.Sprintf("%s added you to %s", owner.Name, repo.Name)
  129. data := ComposeTplData(nil)
  130. data["RepoLink"] = path.Join(owner.Name, repo.Name)
  131. data["Subject"] = subject
  132. body, err := r.HTMLString(string(NOTIFY_COLLABORATOR), data)
  133. if err != nil {
  134. return fmt.Errorf("mail.SendCollaboratorMail(fail to render): %v", err)
  135. }
  136. msg := NewMessage([]string{u.Email}, subject, body)
  137. msg.Info = fmt.Sprintf("UID: %d, send register mail", u.Id)
  138. SendAsync(msg)
  139. return nil
  140. }