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

  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. "bytes"
  7. "fmt"
  8. "html/template"
  9. "path"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/mailer"
  13. "code.gitea.io/gitea/modules/markdown"
  14. "code.gitea.io/gitea/modules/setting"
  15. "gopkg.in/gomail.v2"
  16. "gopkg.in/macaron.v1"
  17. )
  18. const (
  19. mailAuthActivate base.TplName = "auth/activate"
  20. mailAuthActivateEmail base.TplName = "auth/activate_email"
  21. mailAuthResetPassword base.TplName = "auth/reset_passwd"
  22. mailAuthRegisterNotify base.TplName = "auth/register_notify"
  23. mailIssueComment base.TplName = "issue/comment"
  24. mailIssueMention base.TplName = "issue/mention"
  25. mailNotifyCollaborator base.TplName = "notify/collaborator"
  26. )
  27. var templates *template.Template
  28. // InitMailRender initializes the macaron mail renderer
  29. func InitMailRender(tmpls *template.Template) {
  30. templates = tmpls
  31. }
  32. // SendTestMail sends a test mail
  33. func SendTestMail(email string) error {
  34. return gomail.Send(mailer.Sender, mailer.NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").Message)
  35. }
  36. // SendUserMail sends a mail to the user
  37. func SendUserMail(c *macaron.Context, u *User, tpl base.TplName, code, subject, info string) {
  38. data := map[string]interface{}{
  39. "Username": u.DisplayName(),
  40. "ActiveCodeLives": setting.Service.ActiveCodeLives / 60,
  41. "ResetPwdCodeLives": setting.Service.ResetPwdCodeLives / 60,
  42. "Code": code,
  43. }
  44. var content bytes.Buffer
  45. if err := templates.ExecuteTemplate(&content, string(tpl), data); err != nil {
  46. log.Error(3, "Template: %v", err)
  47. return
  48. }
  49. msg := mailer.NewMessage([]string{u.Email}, subject, content.String())
  50. msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)
  51. mailer.SendAsync(msg)
  52. }
  53. // SendActivateAccountMail sends an activation mail to the user
  54. func SendActivateAccountMail(c *macaron.Context, u *User) {
  55. SendUserMail(c, u, mailAuthActivate, u.GenerateActivateCode(), c.Tr("mail.activate_account"), "activate account")
  56. }
  57. // SendResetPasswordMail sends a password reset mail to the user
  58. func SendResetPasswordMail(c *macaron.Context, u *User) {
  59. SendUserMail(c, u, mailAuthResetPassword, u.GenerateActivateCode(), c.Tr("mail.reset_password"), "reset password")
  60. }
  61. // SendActivateEmailMail sends confirmation email.
  62. func SendActivateEmailMail(c *macaron.Context, u *User, email *EmailAddress) {
  63. data := map[string]interface{}{
  64. "Username": u.DisplayName(),
  65. "ActiveCodeLives": setting.Service.ActiveCodeLives / 60,
  66. "Code": u.GenerateEmailActivateCode(email.Email),
  67. "Email": email.Email,
  68. }
  69. var content bytes.Buffer
  70. if err := templates.ExecuteTemplate(&content, string(mailAuthActivateEmail), data); err != nil {
  71. log.Error(3, "Template: %v", err)
  72. return
  73. }
  74. msg := mailer.NewMessage([]string{email.Email}, c.Tr("mail.activate_email"), content.String())
  75. msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
  76. mailer.SendAsync(msg)
  77. }
  78. // SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
  79. func SendRegisterNotifyMail(c *macaron.Context, u *User) {
  80. data := map[string]interface{}{
  81. "Username": u.DisplayName(),
  82. }
  83. var content bytes.Buffer
  84. if err := templates.ExecuteTemplate(&content, string(mailAuthRegisterNotify), data); err != nil {
  85. log.Error(3, "Template: %v", err)
  86. return
  87. }
  88. msg := mailer.NewMessage([]string{u.Email}, c.Tr("mail.register_notify"), content.String())
  89. msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
  90. mailer.SendAsync(msg)
  91. }
  92. // SendCollaboratorMail sends mail notification to new collaborator.
  93. func SendCollaboratorMail(u, doer *User, repo *Repository) {
  94. repoName := path.Join(repo.Owner.Name, repo.Name)
  95. subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repoName)
  96. data := map[string]interface{}{
  97. "Subject": subject,
  98. "RepoName": repoName,
  99. "Link": repo.HTMLURL(),
  100. }
  101. var content bytes.Buffer
  102. if err := templates.ExecuteTemplate(&content, string(mailNotifyCollaborator), data); err != nil {
  103. log.Error(3, "Template: %v", err)
  104. return
  105. }
  106. msg := mailer.NewMessage([]string{u.Email}, subject, content.String())
  107. msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)
  108. mailer.SendAsync(msg)
  109. }
  110. func composeTplData(subject, body, link string) map[string]interface{} {
  111. data := make(map[string]interface{}, 10)
  112. data["Subject"] = subject
  113. data["Body"] = body
  114. data["Link"] = link
  115. return data
  116. }
  117. func composeIssueMessage(issue *Issue, doer *User, tplName base.TplName, tos []string, info string) *mailer.Message {
  118. subject := issue.mailSubject()
  119. body := string(markdown.RenderString(issue.Content, issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
  120. data := composeTplData(subject, body, issue.HTMLURL())
  121. data["Doer"] = doer
  122. var content bytes.Buffer
  123. if err := templates.ExecuteTemplate(&content, string(tplName), data); err != nil {
  124. log.Error(3, "Template: %v", err)
  125. }
  126. msg := mailer.NewMessageFrom(tos, fmt.Sprintf(`"%s" <%s>`, doer.DisplayName(), setting.MailService.FromEmail), subject, content.String())
  127. msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
  128. return msg
  129. }
  130. // SendIssueCommentMail composes and sends issue comment emails to target receivers.
  131. func SendIssueCommentMail(issue *Issue, doer *User, tos []string) {
  132. if len(tos) == 0 {
  133. return
  134. }
  135. mailer.SendAsync(composeIssueMessage(issue, doer, mailIssueComment, tos, "issue comment"))
  136. }
  137. // SendIssueMentionMail composes and sends issue mention emails to target receivers.
  138. func SendIssueMentionMail(issue *Issue, doer *User, tos []string) {
  139. if len(tos) == 0 {
  140. return
  141. }
  142. mailer.SendAsync(composeIssueMessage(issue, doer, mailIssueMention, tos, "issue mention"))
  143. }