mail.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package mailer
  7. import (
  8. "fmt"
  9. "gitote/gitote/pkg/markup"
  10. "gitote/gitote/pkg/setting"
  11. "html/template"
  12. raven "github.com/getsentry/raven-go"
  13. log "gopkg.in/clog.v1"
  14. "gopkg.in/gomail.v2"
  15. "gopkg.in/macaron.v1"
  16. )
  17. // Stores mail templates
  18. const (
  19. MailAuthActivate = "auth/activate"
  20. MailAuthActivateEmail = "auth/activate_email"
  21. MailAuthResetPassword = "auth/reset_passwd"
  22. MailAuthRegisterNotify = "auth/register_notify"
  23. MailIssueComment = "issue/comment"
  24. MailIssueMention = "issue/mention"
  25. MailNotifyCollaborator = "notify/collaborator"
  26. )
  27. // MailRender holds MailRender
  28. type MailRender interface {
  29. HTMLString(string, interface{}, ...macaron.HTMLOptions) (string, error)
  30. }
  31. var mailRender MailRender
  32. // InitMailRender initializes the macaron mail renderer
  33. func InitMailRender(dir, appendDir string, funcMap []template.FuncMap) {
  34. opt := &macaron.RenderOptions{
  35. Directory: dir,
  36. AppendDirectories: []string{appendDir},
  37. Funcs: funcMap,
  38. Extensions: []string{".tmpl", ".html"},
  39. }
  40. ts := macaron.NewTemplateSet()
  41. ts.Set(macaron.DEFAULT_TPL_SET_NAME, opt)
  42. mailRender = &macaron.TplRender{
  43. TemplateSet: ts,
  44. Opt: opt,
  45. }
  46. }
  47. // SendTestMail sends a test mail
  48. func SendTestMail(email string) error {
  49. return gomail.Send(&Sender{}, NewMessage([]string{email}, "Gitote Test Email!", "Gitote Test Email!").Message)
  50. }
  51. // User holds user details
  52. type User interface {
  53. ID() int64
  54. DisplayName() string
  55. Email() string
  56. GenerateActivateCode() string
  57. GenerateEmailActivateCode(string) string
  58. }
  59. // Repository holds repository details
  60. type Repository interface {
  61. FullName() string
  62. HTMLURL() string
  63. ComposeMetas() map[string]string
  64. }
  65. // Issue holds issue details
  66. type Issue interface {
  67. MailSubject() string
  68. Content() string
  69. HTMLURL() string
  70. }
  71. // SendUserMail sends a mail to the user
  72. func SendUserMail(c *macaron.Context, u User, tpl, code, subject, info string) {
  73. data := map[string]interface{}{
  74. "Username": u.DisplayName(),
  75. "ActiveCodeLives": setting.Service.ActiveCodeLives / 60,
  76. "ResetPwdCodeLives": setting.Service.ResetPwdCodeLives / 60,
  77. "Code": code,
  78. }
  79. body, err := mailRender.HTMLString(string(tpl), data)
  80. if err != nil {
  81. raven.CaptureErrorAndWait(err, nil)
  82. log.Error(2, "HTMLString: %v", err)
  83. return
  84. }
  85. msg := NewMessage([]string{u.Email()}, subject, body)
  86. msg.Info = fmt.Sprintf("UID: %d, %s", u.ID(), info)
  87. Send(msg)
  88. }
  89. // SendActivateAccountMail sends an activation mail to the user (new user registration)
  90. func SendActivateAccountMail(c *macaron.Context, u User) {
  91. SendUserMail(c, u, MailAuthActivate, u.GenerateActivateCode(), c.Tr("mail.activate_account"), "activate account")
  92. }
  93. // SendResetPasswordMail sends a password reset mail to the user
  94. func SendResetPasswordMail(c *macaron.Context, u User) {
  95. SendUserMail(c, u, MailAuthResetPassword, u.GenerateActivateCode(), c.Tr("mail.reset_password"), "reset password")
  96. }
  97. // SendActivateEmailMail sends confirmation email.
  98. func SendActivateEmailMail(c *macaron.Context, u User, email string) {
  99. data := map[string]interface{}{
  100. "Username": u.DisplayName(),
  101. "ActiveCodeLives": setting.Service.ActiveCodeLives / 60,
  102. "Code": u.GenerateEmailActivateCode(email),
  103. "Email": email,
  104. }
  105. body, err := mailRender.HTMLString(string(MailAuthActivateEmail), data)
  106. if err != nil {
  107. raven.CaptureErrorAndWait(err, nil)
  108. log.Error(3, "HTMLString: %v", err)
  109. return
  110. }
  111. msg := NewMessage([]string{email}, c.Tr("mail.activate_email"), body)
  112. msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID())
  113. Send(msg)
  114. }
  115. // SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
  116. func SendRegisterNotifyMail(c *macaron.Context, u User) {
  117. data := map[string]interface{}{
  118. "Username": u.DisplayName(),
  119. }
  120. body, err := mailRender.HTMLString(string(MailAuthRegisterNotify), data)
  121. if err != nil {
  122. raven.CaptureErrorAndWait(err, nil)
  123. log.Error(3, "HTMLString: %v", err)
  124. return
  125. }
  126. msg := NewMessage([]string{u.Email()}, c.Tr("mail.register_notify"), body)
  127. msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID())
  128. Send(msg)
  129. }
  130. // SendCollaboratorMail sends mail notification to new collaborator.
  131. func SendCollaboratorMail(u, doer User, repo Repository) {
  132. subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repo.FullName())
  133. data := map[string]interface{}{
  134. "Subject": subject,
  135. "RepoName": repo.FullName(),
  136. "Link": repo.HTMLURL(),
  137. }
  138. body, err := mailRender.HTMLString(string(MailNotifyCollaborator), data)
  139. if err != nil {
  140. raven.CaptureErrorAndWait(err, nil)
  141. log.Error(3, "HTMLString: %v", err)
  142. return
  143. }
  144. msg := NewMessage([]string{u.Email()}, subject, body)
  145. msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID())
  146. Send(msg)
  147. }
  148. func composeTplData(subject, body, link string) map[string]interface{} {
  149. data := make(map[string]interface{}, 10)
  150. data["Subject"] = subject
  151. data["Body"] = body
  152. data["Link"] = link
  153. return data
  154. }
  155. func composeIssueMessage(issue Issue, repo Repository, doer User, tplName string, tos []string, info string) *Message {
  156. subject := issue.MailSubject()
  157. body := string(markup.Markdown([]byte(issue.Content()), repo.HTMLURL(), repo.ComposeMetas()))
  158. data := composeTplData(subject, body, issue.HTMLURL())
  159. data["Doer"] = doer
  160. content, err := mailRender.HTMLString(tplName, data)
  161. if err != nil {
  162. raven.CaptureErrorAndWait(err, nil)
  163. log.Error(3, "HTMLString (%s): %v", tplName, err)
  164. }
  165. from := gomail.NewMessage().FormatAddress(setting.MailService.FromEmail, doer.DisplayName())
  166. msg := NewMessageFrom(tos, from, subject, content)
  167. msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
  168. return msg
  169. }
  170. // SendIssueCommentMail composes and sends issue comment emails to target receivers.
  171. func SendIssueCommentMail(issue Issue, repo Repository, doer User, tos []string) {
  172. if len(tos) == 0 {
  173. return
  174. }
  175. Send(composeIssueMessage(issue, repo, doer, MailIssueComment, tos, "issue comment"))
  176. }
  177. // SendIssueMentionMail composes and sends issue mention emails to target receivers.
  178. func SendIssueMentionMail(issue Issue, repo Repository, doer User, tos []string) {
  179. if len(tos) == 0 {
  180. return
  181. }
  182. Send(composeIssueMessage(issue, repo, doer, MailIssueMention, tos, "issue mention"))
  183. }