您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package mailer
  6. import (
  7. "bytes"
  8. "fmt"
  9. "html/template"
  10. "path"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/markup"
  15. "code.gitea.io/gitea/modules/markup/markdown"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/modules/timeutil"
  18. "gopkg.in/gomail.v2"
  19. )
  20. const (
  21. mailAuthActivate base.TplName = "auth/activate"
  22. mailAuthActivateEmail base.TplName = "auth/activate_email"
  23. mailAuthResetPassword base.TplName = "auth/reset_passwd"
  24. mailAuthRegisterNotify base.TplName = "auth/register_notify"
  25. mailIssueComment base.TplName = "issue/comment"
  26. mailIssueMention base.TplName = "issue/mention"
  27. mailIssueAssigned base.TplName = "issue/assigned"
  28. mailNotifyCollaborator base.TplName = "notify/collaborator"
  29. )
  30. var templates *template.Template
  31. // InitMailRender initializes the mail renderer
  32. func InitMailRender(tmpls *template.Template) {
  33. templates = tmpls
  34. }
  35. // SendTestMail sends a test mail
  36. func SendTestMail(email string) error {
  37. return gomail.Send(Sender, NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").Message)
  38. }
  39. // SendUserMail sends a mail to the user
  40. func SendUserMail(language string, u *models.User, tpl base.TplName, code, subject, info string) {
  41. data := map[string]interface{}{
  42. "DisplayName": u.DisplayName(),
  43. "ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, language),
  44. "ResetPwdCodeLives": timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, language),
  45. "Code": code,
  46. }
  47. var content bytes.Buffer
  48. if err := templates.ExecuteTemplate(&content, string(tpl), data); err != nil {
  49. log.Error("Template: %v", err)
  50. return
  51. }
  52. msg := NewMessage([]string{u.Email}, subject, content.String())
  53. msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)
  54. SendAsync(msg)
  55. }
  56. // Locale represents an interface to translation
  57. type Locale interface {
  58. Language() string
  59. Tr(string, ...interface{}) string
  60. }
  61. // SendActivateAccountMail sends an activation mail to the user (new user registration)
  62. func SendActivateAccountMail(locale Locale, u *models.User) {
  63. SendUserMail(locale.Language(), u, mailAuthActivate, u.GenerateActivateCode(), locale.Tr("mail.activate_account"), "activate account")
  64. }
  65. // SendResetPasswordMail sends a password reset mail to the user
  66. func SendResetPasswordMail(locale Locale, u *models.User) {
  67. SendUserMail(locale.Language(), u, mailAuthResetPassword, u.GenerateActivateCode(), locale.Tr("mail.reset_password"), "recover account")
  68. }
  69. // SendActivateEmailMail sends confirmation email to confirm new email address
  70. func SendActivateEmailMail(locale Locale, u *models.User, email *models.EmailAddress) {
  71. data := map[string]interface{}{
  72. "DisplayName": u.DisplayName(),
  73. "ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, locale.Language()),
  74. "Code": u.GenerateEmailActivateCode(email.Email),
  75. "Email": email.Email,
  76. }
  77. var content bytes.Buffer
  78. if err := templates.ExecuteTemplate(&content, string(mailAuthActivateEmail), data); err != nil {
  79. log.Error("Template: %v", err)
  80. return
  81. }
  82. msg := NewMessage([]string{email.Email}, locale.Tr("mail.activate_email"), content.String())
  83. msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
  84. SendAsync(msg)
  85. }
  86. // SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
  87. func SendRegisterNotifyMail(locale Locale, u *models.User) {
  88. if setting.MailService == nil {
  89. log.Warn("SendRegisterNotifyMail is being invoked but mail service hasn't been initialized")
  90. return
  91. }
  92. data := map[string]interface{}{
  93. "DisplayName": u.DisplayName(),
  94. "Username": u.Name,
  95. }
  96. var content bytes.Buffer
  97. if err := templates.ExecuteTemplate(&content, string(mailAuthRegisterNotify), data); err != nil {
  98. log.Error("Template: %v", err)
  99. return
  100. }
  101. msg := NewMessage([]string{u.Email}, locale.Tr("mail.register_notify"), content.String())
  102. msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
  103. SendAsync(msg)
  104. }
  105. // SendCollaboratorMail sends mail notification to new collaborator.
  106. func SendCollaboratorMail(u, doer *models.User, repo *models.Repository) {
  107. repoName := path.Join(repo.Owner.Name, repo.Name)
  108. subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repoName)
  109. data := map[string]interface{}{
  110. "Subject": subject,
  111. "RepoName": repoName,
  112. "Link": repo.HTMLURL(),
  113. }
  114. var content bytes.Buffer
  115. if err := templates.ExecuteTemplate(&content, string(mailNotifyCollaborator), data); err != nil {
  116. log.Error("Template: %v", err)
  117. return
  118. }
  119. msg := NewMessage([]string{u.Email}, subject, content.String())
  120. msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)
  121. SendAsync(msg)
  122. }
  123. func composeTplData(subject, body, link string) map[string]interface{} {
  124. data := make(map[string]interface{}, 10)
  125. data["Subject"] = subject
  126. data["Body"] = body
  127. data["Link"] = link
  128. return data
  129. }
  130. func composeIssueCommentMessage(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tplName base.TplName, tos []string, info string) *Message {
  131. var subject string
  132. if comment != nil {
  133. subject = "Re: " + mailSubject(issue)
  134. } else {
  135. subject = mailSubject(issue)
  136. }
  137. err := issue.LoadRepo()
  138. if err != nil {
  139. log.Error("LoadRepo: %v", err)
  140. }
  141. body := string(markup.RenderByType(markdown.MarkupName, []byte(content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
  142. var data = make(map[string]interface{}, 10)
  143. if comment != nil {
  144. data = composeTplData(subject, body, issue.HTMLURL()+"#"+comment.HashTag())
  145. } else {
  146. data = composeTplData(subject, body, issue.HTMLURL())
  147. }
  148. data["Doer"] = doer
  149. data["Issue"] = issue
  150. var mailBody bytes.Buffer
  151. if err := templates.ExecuteTemplate(&mailBody, string(tplName), data); err != nil {
  152. log.Error("Template: %v", err)
  153. }
  154. msg := NewMessageFrom(tos, doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
  155. msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
  156. // Set Message-ID on first message so replies know what to reference
  157. if comment == nil {
  158. msg.SetHeader("Message-ID", "<"+issue.ReplyReference()+">")
  159. } else {
  160. msg.SetHeader("In-Reply-To", "<"+issue.ReplyReference()+">")
  161. msg.SetHeader("References", "<"+issue.ReplyReference()+">")
  162. }
  163. return msg
  164. }
  165. // SendIssueCommentMail composes and sends issue comment emails to target receivers.
  166. func SendIssueCommentMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) {
  167. if len(tos) == 0 {
  168. return
  169. }
  170. SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueComment, tos, "issue comment"))
  171. }
  172. // SendIssueMentionMail composes and sends issue mention emails to target receivers.
  173. func SendIssueMentionMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) {
  174. if len(tos) == 0 {
  175. return
  176. }
  177. SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention"))
  178. }
  179. // SendIssueAssignedMail composes and sends issue assigned email
  180. func SendIssueAssignedMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) {
  181. SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueAssigned, tos, "issue assigned"))
  182. }