mail.go 5.8 KB

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