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 7.0KB

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