mail.go 5.9 KB

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