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

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