mail.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. "encoding/hex"
  7. "errors"
  8. "fmt"
  9. "path"
  10. "github.com/Unknwon/com"
  11. "github.com/Unknwon/macaron"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. const (
  18. AUTH_ACTIVE base.TplName = "mail/auth/active"
  19. AUTH_REGISTER_SUCCESS base.TplName = "mail/auth/register_success"
  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. // Create New mail message use MailFrom and MailUser
  25. func NewMailMessageFrom(To []string, from, subject, body string) Message {
  26. return NewHtmlMessage(To, from, subject, body)
  27. }
  28. // Create New mail message use MailFrom and MailUser
  29. func NewMailMessage(To []string, subject, body string) Message {
  30. return NewMailMessageFrom(To, setting.MailService.From, subject, body)
  31. }
  32. func GetMailTmplData(u *models.User) map[interface{}]interface{} {
  33. data := make(map[interface{}]interface{}, 10)
  34. data["AppName"] = setting.AppName
  35. data["AppVer"] = setting.AppVer
  36. data["AppUrl"] = setting.AppUrl
  37. data["ActiveCodeLives"] = setting.Service.ActiveCodeLives / 60
  38. data["ResetPwdCodeLives"] = setting.Service.ResetPwdCodeLives / 60
  39. if u != nil {
  40. data["User"] = u
  41. }
  42. return data
  43. }
  44. // create a time limit code for user active
  45. func CreateUserActiveCode(u *models.User, startInf interface{}) string {
  46. minutes := setting.Service.ActiveCodeLives
  47. data := com.ToStr(u.Id) + u.Email + u.LowerName + u.Passwd + u.Rands
  48. code := base.CreateTimeLimitCode(data, minutes, startInf)
  49. // add tail hex username
  50. code += hex.EncodeToString([]byte(u.LowerName))
  51. return code
  52. }
  53. // Send user register mail with active code
  54. func SendRegisterMail(r macaron.Render, u *models.User) {
  55. code := CreateUserActiveCode(u, nil)
  56. subject := "Register success, Welcome"
  57. data := GetMailTmplData(u)
  58. data["Code"] = code
  59. body, err := r.HTMLString(string(AUTH_REGISTER_SUCCESS), data)
  60. if err != nil {
  61. log.Error(4, "mail.SendRegisterMail(fail to render): %v", err)
  62. return
  63. }
  64. msg := NewMailMessage([]string{u.Email}, subject, body)
  65. msg.Info = fmt.Sprintf("UID: %d, send register mail", u.Id)
  66. SendAsync(&msg)
  67. }
  68. // Send email verify active email.
  69. func SendActiveMail(r macaron.Render, u *models.User) {
  70. code := CreateUserActiveCode(u, nil)
  71. subject := "Verify your e-mail address"
  72. data := GetMailTmplData(u)
  73. data["Code"] = code
  74. body, err := r.HTMLString(string(AUTH_ACTIVE), data)
  75. if err != nil {
  76. log.Error(4, "mail.SendActiveMail(fail to render): %v", err)
  77. return
  78. }
  79. msg := NewMailMessage([]string{u.Email}, subject, body)
  80. msg.Info = fmt.Sprintf("UID: %d, send active mail", u.Id)
  81. SendAsync(&msg)
  82. }
  83. // Send reset password email.
  84. func SendResetPasswdMail(r macaron.Render, u *models.User) {
  85. code := CreateUserActiveCode(u, nil)
  86. subject := "Reset your password"
  87. data := GetMailTmplData(u)
  88. data["Code"] = code
  89. body, err := r.HTMLString(string(AUTH_RESET_PASSWORD), data)
  90. if err != nil {
  91. log.Error(4, "mail.SendResetPasswdMail(fail to render): %v", err)
  92. return
  93. }
  94. msg := NewMailMessage([]string{u.Email}, subject, body)
  95. msg.Info = fmt.Sprintf("UID: %d, send reset password email", u.Id)
  96. SendAsync(&msg)
  97. }
  98. // SendIssueNotifyMail sends mail notification of all watchers of repository.
  99. func SendIssueNotifyMail(u, owner *models.User, repo *models.Repository, issue *models.Issue) ([]string, error) {
  100. ws, err := models.GetWatchers(repo.Id)
  101. if err != nil {
  102. return nil, errors.New("mail.NotifyWatchers(GetWatchers): " + err.Error())
  103. }
  104. tos := make([]string, 0, len(ws))
  105. for i := range ws {
  106. uid := ws[i].UserId
  107. if u.Id == uid {
  108. continue
  109. }
  110. u, err := models.GetUserById(uid)
  111. if err != nil {
  112. return nil, errors.New("mail.NotifyWatchers(GetUserById): " + err.Error())
  113. }
  114. tos = append(tos, u.Email)
  115. }
  116. if len(tos) == 0 {
  117. return tos, nil
  118. }
  119. subject := fmt.Sprintf("[%s] %s(#%d)", repo.Name, issue.Name, issue.Index)
  120. content := fmt.Sprintf("%s<br>-<br> <a href=\"%s%s/%s/issues/%d\">View it on Gogs</a>.",
  121. base.RenderSpecialLink([]byte(issue.Content), owner.Name+"/"+repo.Name),
  122. setting.AppUrl, owner.Name, repo.Name, issue.Index)
  123. msg := NewMailMessageFrom(tos, u.Email, subject, content)
  124. msg.Info = fmt.Sprintf("Subject: %s, send issue notify emails", subject)
  125. SendAsync(&msg)
  126. return tos, nil
  127. }
  128. // SendIssueMentionMail sends mail notification for who are mentioned in issue.
  129. func SendIssueMentionMail(r macaron.Render, u, owner *models.User,
  130. repo *models.Repository, issue *models.Issue, tos []string) error {
  131. if len(tos) == 0 {
  132. return nil
  133. }
  134. subject := fmt.Sprintf("[%s] %s(#%d)", repo.Name, issue.Name, issue.Index)
  135. data := GetMailTmplData(nil)
  136. data["IssueLink"] = fmt.Sprintf("%s/%s/issues/%d", owner.Name, repo.Name, issue.Index)
  137. data["Subject"] = subject
  138. body, err := r.HTMLString(string(NOTIFY_MENTION), data)
  139. if err != nil {
  140. return fmt.Errorf("mail.SendIssueMentionMail(fail to render): %v", err)
  141. }
  142. msg := NewMailMessageFrom(tos, u.Email, subject, body)
  143. msg.Info = fmt.Sprintf("Subject: %s, send issue mention emails", subject)
  144. SendAsync(&msg)
  145. return nil
  146. }
  147. // SendCollaboratorMail sends mail notification to new collaborator.
  148. func SendCollaboratorMail(r macaron.Render, u, owner *models.User,
  149. repo *models.Repository) error {
  150. subject := fmt.Sprintf("%s added you to %s", owner.Name, repo.Name)
  151. data := GetMailTmplData(nil)
  152. data["RepoLink"] = path.Join(owner.Name, repo.Name)
  153. data["Subject"] = subject
  154. body, err := r.HTMLString(string(NOTIFY_COLLABORATOR), data)
  155. if err != nil {
  156. return fmt.Errorf("mail.SendCollaboratorMail(fail to render): %v", err)
  157. }
  158. msg := NewMailMessage([]string{u.Email}, subject, body)
  159. msg.Info = fmt.Sprintf("UID: %d, send register mail", u.Id)
  160. SendAsync(&msg)
  161. return nil
  162. }