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.9 kB

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