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.

mail.go 6.2KB

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