mail.go 7.0 KB

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