mail.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 mailer
  5. import (
  6. "fmt"
  7. "html/template"
  8. log "gopkg.in/clog.v1"
  9. "gopkg.in/gomail.v2"
  10. "gopkg.in/macaron.v1"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/markup"
  13. "github.com/gogits/gogs/modules/setting"
  14. )
  15. const (
  16. MAIL_AUTH_ACTIVATE base.TplName = "auth/activate"
  17. MAIL_AUTH_ACTIVATE_EMAIL base.TplName = "auth/activate_email"
  18. MAIL_AUTH_RESET_PASSWORD base.TplName = "auth/reset_passwd"
  19. MAIL_AUTH_REGISTER_NOTIFY base.TplName = "auth/register_notify"
  20. MAIL_ISSUE_COMMENT base.TplName = "issue/comment"
  21. MAIL_ISSUE_MENTION base.TplName = "issue/mention"
  22. MAIL_NOTIFY_COLLABORATOR base.TplName = "notify/collaborator"
  23. )
  24. type MailRender interface {
  25. HTMLString(string, interface{}, ...macaron.HTMLOptions) (string, error)
  26. }
  27. var mailRender MailRender
  28. func InitMailRender(dir, appendDir string, funcMap []template.FuncMap) {
  29. opt := &macaron.RenderOptions{
  30. Directory: dir,
  31. AppendDirectories: []string{appendDir},
  32. Funcs: funcMap,
  33. Extensions: []string{".tmpl", ".html"},
  34. }
  35. ts := macaron.NewTemplateSet()
  36. ts.Set(macaron.DEFAULT_TPL_SET_NAME, opt)
  37. mailRender = &macaron.TplRender{
  38. TemplateSet: ts,
  39. Opt: opt,
  40. }
  41. }
  42. func SendTestMail(email string) error {
  43. return gomail.Send(&Sender{}, NewMessage([]string{email}, "Gogs Test Email!", "Gogs Test Email!").Message)
  44. }
  45. /*
  46. Setup interfaces of used methods in mail to avoid cycle import.
  47. */
  48. type User interface {
  49. ID() int64
  50. DisplayName() string
  51. Email() string
  52. GenerateActivateCode() string
  53. GenerateEmailActivateCode(string) string
  54. }
  55. type Repository interface {
  56. FullName() string
  57. HTMLURL() string
  58. ComposeMetas() map[string]string
  59. }
  60. type Issue interface {
  61. MailSubject() string
  62. Content() string
  63. HTMLURL() string
  64. }
  65. func SendUserMail(c *macaron.Context, u User, tpl base.TplName, code, subject, info string) {
  66. data := map[string]interface{}{
  67. "Username": u.DisplayName(),
  68. "ActiveCodeLives": setting.Service.ActiveCodeLives / 60,
  69. "ResetPwdCodeLives": setting.Service.ResetPwdCodeLives / 60,
  70. "Code": code,
  71. }
  72. body, err := mailRender.HTMLString(string(tpl), data)
  73. if err != nil {
  74. log.Error(3, "HTMLString: %v", err)
  75. return
  76. }
  77. msg := NewMessage([]string{u.Email()}, subject, body)
  78. msg.Info = fmt.Sprintf("UID: %d, %s", u.ID(), info)
  79. SendAsync(msg)
  80. }
  81. func SendActivateAccountMail(c *macaron.Context, u User) {
  82. SendUserMail(c, u, MAIL_AUTH_ACTIVATE, u.GenerateActivateCode(), c.Tr("mail.activate_account"), "activate account")
  83. }
  84. func SendResetPasswordMail(c *macaron.Context, u User) {
  85. SendUserMail(c, u, MAIL_AUTH_RESET_PASSWORD, u.GenerateActivateCode(), c.Tr("mail.reset_password"), "reset password")
  86. }
  87. // SendActivateAccountMail sends confirmation email.
  88. func SendActivateEmailMail(c *macaron.Context, u User, email string) {
  89. data := map[string]interface{}{
  90. "Username": u.DisplayName(),
  91. "ActiveCodeLives": setting.Service.ActiveCodeLives / 60,
  92. "Code": u.GenerateEmailActivateCode(email),
  93. "Email": email,
  94. }
  95. body, err := mailRender.HTMLString(string(MAIL_AUTH_ACTIVATE_EMAIL), data)
  96. if err != nil {
  97. log.Error(3, "HTMLString: %v", err)
  98. return
  99. }
  100. msg := NewMessage([]string{email}, c.Tr("mail.activate_email"), body)
  101. msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID())
  102. SendAsync(msg)
  103. }
  104. // SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
  105. func SendRegisterNotifyMail(c *macaron.Context, u User) {
  106. data := map[string]interface{}{
  107. "Username": u.DisplayName(),
  108. }
  109. body, err := mailRender.HTMLString(string(MAIL_AUTH_REGISTER_NOTIFY), data)
  110. if err != nil {
  111. log.Error(3, "HTMLString: %v", err)
  112. return
  113. }
  114. msg := NewMessage([]string{u.Email()}, c.Tr("mail.register_notify"), body)
  115. msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID())
  116. SendAsync(msg)
  117. }
  118. // SendCollaboratorMail sends mail notification to new collaborator.
  119. func SendCollaboratorMail(u, doer User, repo Repository) {
  120. subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repo.FullName())
  121. data := map[string]interface{}{
  122. "Subject": subject,
  123. "RepoName": repo.FullName(),
  124. "Link": repo.HTMLURL(),
  125. }
  126. body, err := mailRender.HTMLString(string(MAIL_NOTIFY_COLLABORATOR), data)
  127. if err != nil {
  128. log.Error(3, "HTMLString: %v", err)
  129. return
  130. }
  131. msg := NewMessage([]string{u.Email()}, subject, body)
  132. msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID())
  133. SendAsync(msg)
  134. }
  135. func composeTplData(subject, body, link string) map[string]interface{} {
  136. data := make(map[string]interface{}, 10)
  137. data["Subject"] = subject
  138. data["Body"] = body
  139. data["Link"] = link
  140. return data
  141. }
  142. func composeIssueMessage(issue Issue, repo Repository, doer User, tplName base.TplName, tos []string, info string) *Message {
  143. subject := issue.MailSubject()
  144. body := string(markup.RenderSpecialLink([]byte(issue.Content()), repo.HTMLURL(), repo.ComposeMetas()))
  145. data := composeTplData(subject, body, issue.HTMLURL())
  146. data["Doer"] = doer
  147. content, err := mailRender.HTMLString(string(tplName), data)
  148. if err != nil {
  149. log.Error(3, "HTMLString (%s): %v", tplName, err)
  150. }
  151. from := gomail.NewMessage().FormatAddress(setting.MailService.FromEmail, doer.DisplayName())
  152. msg := NewMessageFrom(tos, from, subject, content)
  153. msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
  154. return msg
  155. }
  156. // SendIssueCommentMail composes and sends issue comment emails to target receivers.
  157. func SendIssueCommentMail(issue Issue, repo Repository, doer User, tos []string) {
  158. if len(tos) == 0 {
  159. return
  160. }
  161. SendAsync(composeIssueMessage(issue, repo, doer, MAIL_ISSUE_COMMENT, tos, "issue comment"))
  162. }
  163. // SendIssueMentionMail composes and sends issue mention emails to target receivers.
  164. func SendIssueMentionMail(issue Issue, repo Repository, doer User, tos []string) {
  165. if len(tos) == 0 {
  166. return
  167. }
  168. SendAsync(composeIssueMessage(issue, repo, doer, MAIL_ISSUE_MENTION, tos, "issue mention"))
  169. }