mail.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/G-Node/gogs/pkg/markup"
  12. "github.com/G-Node/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_AUTH_INVITE_NOTIFY = "auth/invite_email"
  20. MAIL_ISSUE_COMMENT = "issue/comment"
  21. MAIL_ISSUE_MENTION = "issue/mention"
  22. MAIL_NOTIFY_COLLABORATOR = "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, 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. "Account": c.Data["Account"],
  72. "Inviter": c.Data["Inviter"],
  73. "Pw": c.Data["Pw"],
  74. }
  75. body, err := mailRender.HTMLString(string(tpl), data)
  76. if err != nil {
  77. log.Error(2, "HTMLString: %v", err)
  78. return
  79. }
  80. msg := NewMessage([]string{u.Email()}, subject, body)
  81. msg.Info = fmt.Sprintf("UID: %d, %s", u.ID(), info)
  82. SendAsync(msg)
  83. }
  84. func SendActivateAccountMail(c *macaron.Context, u User) {
  85. SendUserMail(c, u, MAIL_AUTH_ACTIVATE, u.GenerateActivateCode(), c.Tr("mail.activate_account"), "activate account")
  86. }
  87. func SendInviteMail(c *macaron.Context, u User) {
  88. SendUserMail(c, u, MAIL_AUTH_INVITE_NOTIFY, u.GenerateActivateCode(), c.Tr("mail.invite_account"), "invite account")
  89. }
  90. func SendResetPasswordMail(c *macaron.Context, u User) {
  91. SendUserMail(c, u, MAIL_AUTH_RESET_PASSWORD, u.GenerateActivateCode(), c.Tr("mail.reset_password"), "reset password")
  92. }
  93. // SendActivateAccountMail sends confirmation email.
  94. func SendActivateEmailMail(c *macaron.Context, u User, email string) {
  95. data := map[string]interface{}{
  96. "Username": u.DisplayName(),
  97. "ActiveCodeLives": setting.Service.ActiveCodeLives / 60,
  98. "Code": u.GenerateEmailActivateCode(email),
  99. "Email": email,
  100. }
  101. body, err := mailRender.HTMLString(string(MAIL_AUTH_ACTIVATE_EMAIL), data)
  102. if err != nil {
  103. log.Error(3, "HTMLString: %v", err)
  104. return
  105. }
  106. msg := NewMessage([]string{email}, c.Tr("mail.activate_email"), body)
  107. msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID())
  108. SendAsync(msg)
  109. }
  110. // SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
  111. func SendRegisterNotifyMail(c *macaron.Context, u User) {
  112. data := map[string]interface{}{
  113. "Username": u.DisplayName(),
  114. }
  115. body, err := mailRender.HTMLString(string(MAIL_AUTH_REGISTER_NOTIFY), data)
  116. if err != nil {
  117. log.Error(3, "HTMLString: %v", err)
  118. return
  119. }
  120. msg := NewMessage([]string{u.Email()}, c.Tr("mail.register_notify"), body)
  121. msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID())
  122. SendAsync(msg)
  123. }
  124. // SendCollaboratorMail sends mail notification to new collaborator.
  125. func SendCollaboratorMail(u, doer User, repo Repository) {
  126. subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repo.FullName())
  127. data := map[string]interface{}{
  128. "Subject": subject,
  129. "RepoName": repo.FullName(),
  130. "Link": repo.HTMLURL(),
  131. }
  132. body, err := mailRender.HTMLString(string(MAIL_NOTIFY_COLLABORATOR), data)
  133. if err != nil {
  134. log.Error(3, "HTMLString: %v", err)
  135. return
  136. }
  137. msg := NewMessage([]string{u.Email()}, subject, body)
  138. msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID())
  139. SendAsync(msg)
  140. }
  141. func composeTplData(subject, body, link string) map[string]interface{} {
  142. data := make(map[string]interface{}, 10)
  143. data["Subject"] = subject
  144. data["Body"] = body
  145. data["Link"] = link
  146. return data
  147. }
  148. func composeIssueMessage(issue Issue, repo Repository, doer User, tplName string, tos []string, info string) *Message {
  149. subject := issue.MailSubject()
  150. body := string(markup.RenderSpecialLink([]byte(issue.Content()), repo.HTMLURL(), repo.ComposeMetas()))
  151. data := composeTplData(subject, body, issue.HTMLURL())
  152. data["Doer"] = doer
  153. content, err := mailRender.HTMLString(tplName, data)
  154. if err != nil {
  155. log.Error(3, "HTMLString (%s): %v", tplName, err)
  156. }
  157. from := gomail.NewMessage().FormatAddress(setting.MailService.FromEmail, doer.DisplayName())
  158. msg := NewMessageFrom(tos, from, subject, content)
  159. msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
  160. return msg
  161. }
  162. // SendIssueCommentMail composes and sends issue comment emails to target receivers.
  163. func SendIssueCommentMail(issue Issue, repo Repository, doer User, tos []string) {
  164. if len(tos) == 0 {
  165. return
  166. }
  167. SendAsync(composeIssueMessage(issue, repo, doer, MAIL_ISSUE_COMMENT, tos, "issue comment"))
  168. }
  169. // SendIssueMentionMail composes and sends issue mention emails to target receivers.
  170. func SendIssueMentionMail(issue Issue, repo Repository, doer User, tos []string) {
  171. if len(tos) == 0 {
  172. return
  173. }
  174. SendAsync(composeIssueMessage(issue, repo, doer, MAIL_ISSUE_MENTION, tos, "issue mention"))
  175. }