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.

183 lines
5.7 KiB

8 years ago
  1. // Copyright 2016 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 models
  5. import (
  6. "fmt"
  7. "html/template"
  8. "path"
  9. "gopkg.in/gomail.v2"
  10. "gopkg.in/macaron.v1"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/modules/mailer"
  14. "github.com/gogits/gogs/modules/markdown"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. const (
  18. MAIL_AUTH_ACTIVATE base.TplName = "auth/activate"
  19. MAIL_AUTH_ACTIVATE_EMAIL base.TplName = "auth/activate_email"
  20. MAIL_AUTH_RESET_PASSWORD base.TplName = "auth/reset_passwd"
  21. MAIL_AUTH_REGISTER_NOTIFY base.TplName = "auth/register_notify"
  22. MAIL_ISSUE_COMMENT base.TplName = "issue/comment"
  23. MAIL_ISSUE_MENTION base.TplName = "issue/mention"
  24. MAIL_NOTIFY_COLLABORATOR base.TplName = "notify/collaborator"
  25. )
  26. type MailRender interface {
  27. HTMLString(string, interface{}, ...macaron.HTMLOptions) (string, error)
  28. }
  29. var mailRender MailRender
  30. func InitMailRender(dir, appendDir string, funcMap []template.FuncMap) {
  31. opt := &macaron.RenderOptions{
  32. Directory: dir,
  33. AppendDirectories: []string{appendDir},
  34. Funcs: funcMap,
  35. Extensions: []string{".tmpl", ".html"},
  36. }
  37. ts := macaron.NewTemplateSet()
  38. ts.Set(macaron.DEFAULT_TPL_SET_NAME, opt)
  39. mailRender = &macaron.TplRender{
  40. TemplateSet: ts,
  41. Opt: opt,
  42. }
  43. }
  44. func SendTestMail(email string) error {
  45. return gomail.Send(&mailer.Sender{}, mailer.NewMessage([]string{email}, "Gogs Test Email!", "Gogs Test Email!").Message)
  46. }
  47. func SendUserMail(c *macaron.Context, u *User, tpl base.TplName, code, subject, info string) {
  48. data := map[string]interface{}{
  49. "Username": u.DisplayName(),
  50. "ActiveCodeLives": setting.Service.ActiveCodeLives / 60,
  51. "ResetPwdCodeLives": setting.Service.ResetPwdCodeLives / 60,
  52. "Code": code,
  53. }
  54. body, err := mailRender.HTMLString(string(tpl), data)
  55. if err != nil {
  56. log.Error(3, "HTMLString: %v", err)
  57. return
  58. }
  59. msg := mailer.NewMessage([]string{u.Email}, subject, body)
  60. msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)
  61. mailer.SendAsync(msg)
  62. }
  63. func SendActivateAccountMail(c *macaron.Context, u *User) {
  64. SendUserMail(c, u, MAIL_AUTH_ACTIVATE, u.GenerateActivateCode(), c.Tr("mail.activate_account"), "activate account")
  65. }
  66. func SendResetPasswordMail(c *macaron.Context, u *User) {
  67. SendUserMail(c, u, MAIL_AUTH_RESET_PASSWORD, u.GenerateActivateCode(), c.Tr("mail.reset_password"), "reset password")
  68. }
  69. // SendActivateAccountMail sends confirmation email.
  70. func SendActivateEmailMail(c *macaron.Context, u *User, email *EmailAddress) {
  71. data := map[string]interface{}{
  72. "Username": u.DisplayName(),
  73. "ActiveCodeLives": setting.Service.ActiveCodeLives / 60,
  74. "Code": u.GenerateEmailActivateCode(email.Email),
  75. "Email": email.Email,
  76. }
  77. body, err := mailRender.HTMLString(string(MAIL_AUTH_ACTIVATE_EMAIL), data)
  78. if err != nil {
  79. log.Error(3, "HTMLString: %v", err)
  80. return
  81. }
  82. msg := mailer.NewMessage([]string{email.Email}, c.Tr("mail.activate_email"), body)
  83. msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
  84. mailer.SendAsync(msg)
  85. }
  86. // SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
  87. func SendRegisterNotifyMail(c *macaron.Context, u *User) {
  88. data := map[string]interface{}{
  89. "Username": u.DisplayName(),
  90. }
  91. body, err := mailRender.HTMLString(string(MAIL_AUTH_REGISTER_NOTIFY), data)
  92. if err != nil {
  93. log.Error(3, "HTMLString: %v", err)
  94. return
  95. }
  96. msg := mailer.NewMessage([]string{u.Email}, c.Tr("mail.register_notify"), body)
  97. msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
  98. mailer.SendAsync(msg)
  99. }
  100. // SendCollaboratorMail sends mail notification to new collaborator.
  101. func SendCollaboratorMail(u, doer *User, repo *Repository) {
  102. repoName := path.Join(repo.Owner.Name, repo.Name)
  103. subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repoName)
  104. data := map[string]interface{}{
  105. "Subject": subject,
  106. "RepoName": repoName,
  107. "Link": repo.HTMLURL(),
  108. }
  109. body, err := mailRender.HTMLString(string(MAIL_NOTIFY_COLLABORATOR), data)
  110. if err != nil {
  111. log.Error(3, "HTMLString: %v", err)
  112. return
  113. }
  114. msg := mailer.NewMessage([]string{u.Email}, subject, body)
  115. msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)
  116. mailer.SendAsync(msg)
  117. }
  118. func composeTplData(subject, body, link string) map[string]interface{} {
  119. data := make(map[string]interface{}, 10)
  120. data["Subject"] = subject
  121. data["Body"] = body
  122. data["Link"] = link
  123. return data
  124. }
  125. func composeIssueMessage(issue *Issue, doer *User, tplName base.TplName, tos []string, info string) *mailer.Message {
  126. subject := issue.MailSubject()
  127. body := string(markdown.RenderSpecialLink([]byte(issue.Content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
  128. data := composeTplData(subject, body, issue.HTMLURL())
  129. data["Doer"] = doer
  130. content, err := mailRender.HTMLString(string(tplName), data)
  131. if err != nil {
  132. log.Error(3, "HTMLString (%s): %v", tplName, err)
  133. }
  134. msg := mailer.NewMessageFrom(tos, fmt.Sprintf(`"%s" <%s>`, doer.DisplayName(), setting.MailService.User), subject, content)
  135. msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
  136. return msg
  137. }
  138. // SendIssueCommentMail composes and sends issue comment emails to target receivers.
  139. func SendIssueCommentMail(issue *Issue, doer *User, tos []string) {
  140. if len(tos) == 0 {
  141. return
  142. }
  143. mailer.SendAsync(composeIssueMessage(issue, doer, MAIL_ISSUE_COMMENT, tos, "issue comment"))
  144. }
  145. // SendIssueMentionMail composes and sends issue mention emails to target receivers.
  146. func SendIssueMentionMail(issue *Issue, doer *User, tos []string) {
  147. if len(tos) == 0 {
  148. return
  149. }
  150. mailer.SendAsync(composeIssueMessage(issue, doer, MAIL_ISSUE_MENTION, tos, "issue mention"))
  151. }