本站源代码
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.

266 lines
8.6KB

  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. "strconv"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/markup"
  16. "code.gitea.io/gitea/modules/markup/markdown"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/timeutil"
  19. "gopkg.in/gomail.v2"
  20. )
  21. const (
  22. mailAuthActivate base.TplName = "auth/activate"
  23. mailAuthActivateEmail base.TplName = "auth/activate_email"
  24. mailAuthResetPassword base.TplName = "auth/reset_passwd"
  25. mailAuthRegisterNotify base.TplName = "auth/register_notify"
  26. mailIssueComment base.TplName = "issue/comment"
  27. mailIssueMention base.TplName = "issue/mention"
  28. mailIssueAssigned base.TplName = "issue/assigned"
  29. mailNotifyCollaborator base.TplName = "notify/collaborator"
  30. )
  31. var templates *template.Template
  32. // InitMailRender initializes the mail renderer
  33. func InitMailRender(tmpls *template.Template) {
  34. templates = tmpls
  35. }
  36. // SendTestMail sends a test mail
  37. func SendTestMail(email string) error {
  38. return gomail.Send(Sender, NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").Message)
  39. }
  40. // SendUserMail sends a mail to the user
  41. func SendUserMail(language string, u *models.User, tpl base.TplName, code, subject, info string) {
  42. data := map[string]interface{}{
  43. "DisplayName": u.DisplayName(),
  44. "ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, language),
  45. "ResetPwdCodeLives": timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, language),
  46. "Code": code,
  47. }
  48. var content bytes.Buffer
  49. if err := templates.ExecuteTemplate(&content, string(tpl), data); err != nil {
  50. log.Error("Template: %v", err)
  51. return
  52. }
  53. msg := NewMessage([]string{u.Email}, subject, content.String())
  54. msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)
  55. SendAsync(msg)
  56. }
  57. // Locale represents an interface to translation
  58. type Locale interface {
  59. Language() string
  60. Tr(string, ...interface{}) string
  61. }
  62. // SendActivateAccountMail sends an activation mail to the user (new user registration)
  63. func SendActivateAccountMail(locale Locale, u *models.User) {
  64. SendUserMail(locale.Language(), u, mailAuthActivate, u.GenerateActivateCode(), locale.Tr("mail.activate_account"), "activate account")
  65. }
  66. // SendResetPasswordMail sends a password reset mail to the user
  67. func SendResetPasswordMail(locale Locale, u *models.User) {
  68. SendUserMail(locale.Language(), u, mailAuthResetPassword, u.GenerateActivateCode(), locale.Tr("mail.reset_password"), "recover account")
  69. }
  70. // SendActivateEmailMail sends confirmation email to confirm new email address
  71. func SendActivateEmailMail(locale Locale, u *models.User, email *models.EmailAddress) {
  72. data := map[string]interface{}{
  73. "DisplayName": u.DisplayName(),
  74. "ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, locale.Language()),
  75. "Code": u.GenerateEmailActivateCode(email.Email),
  76. "Email": email.Email,
  77. }
  78. var content bytes.Buffer
  79. if err := templates.ExecuteTemplate(&content, string(mailAuthActivateEmail), data); err != nil {
  80. log.Error("Template: %v", err)
  81. return
  82. }
  83. msg := NewMessage([]string{email.Email}, locale.Tr("mail.activate_email"), content.String())
  84. msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
  85. SendAsync(msg)
  86. }
  87. // SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
  88. func SendRegisterNotifyMail(locale Locale, u *models.User) {
  89. if setting.MailService == nil {
  90. log.Warn("SendRegisterNotifyMail is being invoked but mail service hasn't been initialized")
  91. return
  92. }
  93. data := map[string]interface{}{
  94. "DisplayName": u.DisplayName(),
  95. "Username": u.Name,
  96. }
  97. var content bytes.Buffer
  98. if err := templates.ExecuteTemplate(&content, string(mailAuthRegisterNotify), data); err != nil {
  99. log.Error("Template: %v", err)
  100. return
  101. }
  102. msg := NewMessage([]string{u.Email}, locale.Tr("mail.register_notify"), content.String())
  103. msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
  104. SendAsync(msg)
  105. }
  106. // SendCollaboratorMail sends mail notification to new collaborator.
  107. func SendCollaboratorMail(u, doer *models.User, repo *models.Repository) {
  108. repoName := path.Join(repo.Owner.Name, repo.Name)
  109. subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repoName)
  110. data := map[string]interface{}{
  111. "Subject": subject,
  112. "RepoName": repoName,
  113. "Link": repo.HTMLURL(),
  114. }
  115. var content bytes.Buffer
  116. if err := templates.ExecuteTemplate(&content, string(mailNotifyCollaborator), data); err != nil {
  117. log.Error("Template: %v", err)
  118. return
  119. }
  120. msg := NewMessage([]string{u.Email}, subject, content.String())
  121. msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)
  122. SendAsync(msg)
  123. }
  124. func composeTplData(subject, body, link string) map[string]interface{} {
  125. data := make(map[string]interface{}, 10)
  126. data["Subject"] = subject
  127. data["Body"] = body
  128. data["Link"] = link
  129. return data
  130. }
  131. func composeIssueCommentMessage(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tplName base.TplName, tos []string, info string) *Message {
  132. var subject string
  133. if comment != nil {
  134. subject = "Re: " + mailSubject(issue)
  135. } else {
  136. subject = mailSubject(issue)
  137. }
  138. err := issue.LoadRepo()
  139. if err != nil {
  140. log.Error("LoadRepo: %v", err)
  141. }
  142. body := string(markup.RenderByType(markdown.MarkupName, []byte(content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
  143. var data = make(map[string]interface{}, 10)
  144. if comment != nil {
  145. data = composeTplData(subject, body, issue.HTMLURL()+"#"+comment.HashTag())
  146. } else {
  147. data = composeTplData(subject, body, issue.HTMLURL())
  148. }
  149. data["Doer"] = doer
  150. data["Issue"] = issue
  151. var mailBody bytes.Buffer
  152. if err := templates.ExecuteTemplate(&mailBody, string(tplName), data); err != nil {
  153. log.Error("Template: %v", err)
  154. }
  155. msg := NewMessageFrom(tos, doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
  156. msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
  157. // Set Message-ID on first message so replies know what to reference
  158. if comment == nil {
  159. msg.SetHeader("Message-ID", "<"+issue.ReplyReference()+">")
  160. } else {
  161. msg.SetHeader("In-Reply-To", "<"+issue.ReplyReference()+">")
  162. msg.SetHeader("References", "<"+issue.ReplyReference()+">")
  163. }
  164. return msg
  165. }
  166. // SendIssueCommentMail composes and sends issue comment emails to target receivers.
  167. func SendIssueCommentMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) {
  168. if len(tos) == 0 {
  169. return
  170. }
  171. SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueComment, tos, "issue comment"))
  172. }
  173. // SendIssueMentionMail composes and sends issue mention emails to target receivers.
  174. func SendIssueMentionMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) {
  175. if len(tos) == 0 {
  176. return
  177. }
  178. SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention"))
  179. }
  180. // SendIssueAssignedMail composes and sends issue assigned email
  181. func SendIssueAssignedMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) {
  182. SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueAssigned, tos, "issue assigned"))
  183. }
  184. // 发送接受积分邮件
  185. func SendRecievePointNotifyMail(sender, receiver *models.User, transPoint int, why string) {
  186. if setting.MailService == nil {
  187. log.Warn("SendPointNotifyMail is being invoked but mail service hasn't been initialized")
  188. return
  189. }
  190. //剩余积分
  191. var remainingPoint = int(receiver.Point) + transPoint
  192. var senderDisplayName = sender.DisplayName()
  193. var receiverDisplayNmae = receiver.DisplayName()
  194. data := map[string]interface{}{
  195. "ReceiverDisplayName": receiverDisplayNmae,
  196. "ReceiverUserName": receiver.Name,
  197. "ReceiverPoint": transPoint,
  198. "RemainingPoint": remainingPoint,
  199. "SenderDisplayName": senderDisplayName,
  200. "SenderUserName": sender.Name,
  201. "Why": why,
  202. }
  203. var content bytes.Buffer
  204. if err := templates.ExecuteTemplate(&content, "notify/received_point", data); err != nil {
  205. log.Error("Template: %v", err)
  206. return
  207. }
  208. subject := "[通知]" + senderDisplayName + "转了" + strconv.Itoa(transPoint) + "积分给" + receiverDisplayNmae
  209. msg := NewMessage([]string{receiver.Email}, subject, content.String())
  210. msg.Info = fmt.Sprintf("UID: %d, received point notify", receiver.ID)
  211. SendAsync(msg)
  212. }
上海开阖软件有限公司 沪ICP备12045867号-1