mail.go 7.0 KB

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