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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. )
  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 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(language string, u *User, tpl base.TplName, code, subject, info string) {
  38. data := map[string]interface{}{
  39. "DisplayName": u.DisplayName(),
  40. "ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives, language),
  41. "ResetPwdCodeLives": base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, language),
  42. "Code": code,
  43. }
  44. var content bytes.Buffer
  45. if err := templates.ExecuteTemplate(&content, string(tpl), data); err != nil {
  46. log.Error("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. // Locale represents an interface to translation
  54. type Locale interface {
  55. Language() string
  56. Tr(string, ...interface{}) string
  57. }
  58. // SendActivateAccountMail sends an activation mail to the user (new user registration)
  59. func SendActivateAccountMail(locale Locale, u *User) {
  60. SendUserMail(locale.Language(), u, mailAuthActivate, u.GenerateActivateCode(), locale.Tr("mail.activate_account"), "activate account")
  61. }
  62. // SendResetPasswordMail sends a password reset mail to the user
  63. func SendResetPasswordMail(locale Locale, u *User) {
  64. SendUserMail(locale.Language(), u, mailAuthResetPassword, u.GenerateActivateCode(), locale.Tr("mail.reset_password"), "recover account")
  65. }
  66. // SendActivateEmailMail sends confirmation email to confirm new email address
  67. func SendActivateEmailMail(locale Locale, u *User, email *EmailAddress) {
  68. data := map[string]interface{}{
  69. "DisplayName": u.DisplayName(),
  70. "ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives, locale.Language()),
  71. "Code": u.GenerateEmailActivateCode(email.Email),
  72. "Email": email.Email,
  73. }
  74. var content bytes.Buffer
  75. if err := templates.ExecuteTemplate(&content, string(mailAuthActivateEmail), data); err != nil {
  76. log.Error("Template: %v", err)
  77. return
  78. }
  79. msg := mailer.NewMessage([]string{email.Email}, locale.Tr("mail.activate_email"), content.String())
  80. msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
  81. mailer.SendAsync(msg)
  82. }
  83. // SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
  84. func SendRegisterNotifyMail(locale Locale, u *User) {
  85. data := map[string]interface{}{
  86. "DisplayName": u.DisplayName(),
  87. "Username": u.Name,
  88. }
  89. var content bytes.Buffer
  90. if err := templates.ExecuteTemplate(&content, string(mailAuthRegisterNotify), data); err != nil {
  91. log.Error("Template: %v", err)
  92. return
  93. }
  94. msg := mailer.NewMessage([]string{u.Email}, locale.Tr("mail.register_notify"), content.String())
  95. msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
  96. mailer.SendAsync(msg)
  97. }
  98. // SendCollaboratorMail sends mail notification to new collaborator.
  99. func SendCollaboratorMail(u, doer *User, repo *Repository) {
  100. repoName := path.Join(repo.Owner.Name, repo.Name)
  101. subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repoName)
  102. data := map[string]interface{}{
  103. "Subject": subject,
  104. "RepoName": repoName,
  105. "Link": repo.HTMLURL(),
  106. }
  107. var content bytes.Buffer
  108. if err := templates.ExecuteTemplate(&content, string(mailNotifyCollaborator), data); err != nil {
  109. log.Error("Template: %v", err)
  110. return
  111. }
  112. msg := mailer.NewMessage([]string{u.Email}, subject, content.String())
  113. msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)
  114. mailer.SendAsync(msg)
  115. }
  116. func composeTplData(subject, body, link string) map[string]interface{} {
  117. data := make(map[string]interface{}, 10)
  118. data["Subject"] = subject
  119. data["Body"] = body
  120. data["Link"] = link
  121. return data
  122. }
  123. func composeIssueCommentMessage(issue *Issue, doer *User, content string, comment *Comment, tplName base.TplName, tos []string, info string) *mailer.Message {
  124. subject := issue.mailSubject()
  125. err := issue.LoadRepo()
  126. if err != nil {
  127. log.Error("LoadRepo: %v", err)
  128. }
  129. body := string(markup.RenderByType(markdown.MarkupName, []byte(content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
  130. var data = make(map[string]interface{}, 10)
  131. if comment != nil {
  132. data = composeTplData(subject, body, issue.HTMLURL()+"#"+comment.HashTag())
  133. } else {
  134. data = composeTplData(subject, body, issue.HTMLURL())
  135. }
  136. data["Doer"] = doer
  137. var mailBody bytes.Buffer
  138. if err := templates.ExecuteTemplate(&mailBody, string(tplName), data); err != nil {
  139. log.Error("Template: %v", err)
  140. }
  141. msg := mailer.NewMessageFrom(tos, doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
  142. msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
  143. return msg
  144. }
  145. // SendIssueCommentMail composes and sends issue comment emails to target receivers.
  146. func SendIssueCommentMail(issue *Issue, doer *User, content string, comment *Comment, tos []string) {
  147. if len(tos) == 0 {
  148. return
  149. }
  150. mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueComment, tos, "issue comment"))
  151. }
  152. // SendIssueMentionMail composes and sends issue mention emails to target receivers.
  153. func SendIssueMentionMail(issue *Issue, doer *User, content string, comment *Comment, tos []string) {
  154. if len(tos) == 0 {
  155. return
  156. }
  157. mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention"))
  158. }