mail.go 6.0 KB

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