email.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 email
  5. import (
  6. "fmt"
  7. "gogs.io/gogs/internal/tool"
  8. "html/template"
  9. "path/filepath"
  10. "sync"
  11. "time"
  12. "gopkg.in/gomail.v2"
  13. "gopkg.in/macaron.v1"
  14. log "unknwon.dev/clog/v2"
  15. "gogs.io/gogs/internal/conf"
  16. "gogs.io/gogs/internal/markup"
  17. "gogs.io/gogs/templates"
  18. )
  19. const (
  20. MAIL_AUTH_ACTIVATE = "auth/activate"
  21. MAIL_AUTH_ACTIVATE_EMAIL = "auth/activate_email"
  22. MAIL_AUTH_RESET_PASSWORD = "auth/reset_passwd"
  23. MAIL_AUTH_REGISTER_NOTIFY = "auth/register_notify"
  24. MAIL_ISSUE_COMMENT = "issue/comment"
  25. MAIL_ISSUE_MENTION = "issue/mention"
  26. MAIL_NOTIFY_COLLABORATOR = "notify/collaborator"
  27. )
  28. var (
  29. tplRender *macaron.TplRender
  30. tplRenderOnce sync.Once
  31. )
  32. // render renders a mail template with given data.
  33. func render(tpl string, data map[string]any) (string, error) {
  34. tplRenderOnce.Do(func() {
  35. customDir := filepath.Join(conf.CustomDir(), "templates")
  36. opt := &macaron.RenderOptions{
  37. Directory: filepath.Join(conf.WorkDir(), "templates", "mail"),
  38. AppendDirectories: []string{filepath.Join(customDir, "mail")},
  39. Extensions: []string{".tmpl", ".html"},
  40. Funcs: []template.FuncMap{map[string]any{
  41. "AppName": func() string {
  42. return conf.App.BrandName
  43. },
  44. "AppURL": func() string {
  45. return conf.Server.ExternalURL
  46. },
  47. "Year": func() int {
  48. return time.Now().Year()
  49. },
  50. "Str2HTML": func(raw string) template.HTML {
  51. return template.HTML(markup.Sanitize(raw))
  52. },
  53. }},
  54. }
  55. if !conf.Server.LoadAssetsFromDisk {
  56. opt.TemplateFileSystem = templates.NewTemplateFileSystem("mail", customDir)
  57. }
  58. ts := macaron.NewTemplateSet()
  59. ts.Set(macaron.DEFAULT_TPL_SET_NAME, opt)
  60. tplRender = &macaron.TplRender{
  61. TemplateSet: ts,
  62. Opt: opt,
  63. }
  64. })
  65. return tplRender.HTMLString(tpl, data)
  66. }
  67. func SendTestMail(email string) error {
  68. return gomail.Send(&Sender{}, NewMessage([]string{email}, "Gogs Test Email", "Hello 👋, greeting from Gogs!").Message)
  69. }
  70. /*
  71. Setup interfaces of used methods in mail to avoid cycle import.
  72. */
  73. type User interface {
  74. ID() int64
  75. DisplayName() string
  76. Email() string
  77. PublicEmail() string
  78. }
  79. type Repository interface {
  80. FullName() string
  81. HTMLURL() string
  82. ComposeMetas() map[string]string
  83. }
  84. type Issue interface {
  85. MailSubject() string
  86. Content() string
  87. HTMLURL() string
  88. }
  89. func SendUserMail(_ *macaron.Context, u User, tpl, code, subject, info string) {
  90. data := map[string]any{
  91. "Username": u.DisplayName(),
  92. "ActiveCodeLives": conf.Auth.ActivateCodeLives / 60,
  93. "ResetPwdCodeLives": conf.Auth.ResetPasswordCodeLives / 60,
  94. "Code": code,
  95. }
  96. body, err := render(tpl, data)
  97. if err != nil {
  98. log.Error("render: %v", err)
  99. return
  100. }
  101. msg := NewMessage([]string{u.Email()}, subject, body)
  102. msg.Info = fmt.Sprintf("UID: %d, %s", u.ID(), info)
  103. Send(msg)
  104. }
  105. func SendActivateAccountMail(c *macaron.Context, u User) {
  106. token, err := tool.NewClaims(u.ID(), u.Email(), tool.SubjectActiveAccount).ToToken()
  107. if err != nil {
  108. log.Error("Create token error: %s", err.Error())
  109. return
  110. }
  111. SendUserMail(c, u, MAIL_AUTH_ACTIVATE, token, c.Tr("mail.activate_account"), "activate account")
  112. }
  113. func SendResetPasswordMail(c *macaron.Context, u User) {
  114. token, err := tool.NewClaims(u.ID(), u.Email(), tool.SubjectForgetPasswd).ToToken()
  115. if err != nil {
  116. log.Error("Create token error: %s", err.Error())
  117. return
  118. }
  119. SendUserMail(c, u, MAIL_AUTH_RESET_PASSWORD, token, c.Tr("mail.reset_password"), "reset password")
  120. }
  121. // SendActivateAccountMail sends confirmation email.
  122. func SendActivateEmailMail(c *macaron.Context, u User, email string) {
  123. token, err := tool.NewClaims(u.ID(), email, tool.SubjectActiveEmail).ToToken()
  124. if err != nil {
  125. log.Error("Create token error: %s", err.Error())
  126. return
  127. }
  128. data := map[string]any{
  129. "Username": u.DisplayName(),
  130. "ActiveCodeLives": conf.Auth.ActivateCodeLives / 60,
  131. "Code": token,
  132. "Email": email,
  133. }
  134. body, err := render(MAIL_AUTH_ACTIVATE_EMAIL, data)
  135. if err != nil {
  136. log.Error("HTMLString: %v", err)
  137. return
  138. }
  139. msg := NewMessage([]string{email}, c.Tr("mail.activate_email"), body)
  140. msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID())
  141. Send(msg)
  142. }
  143. // SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
  144. func SendRegisterNotifyMail(c *macaron.Context, u User) {
  145. data := map[string]any{
  146. "Username": u.DisplayName(),
  147. }
  148. body, err := render(MAIL_AUTH_REGISTER_NOTIFY, data)
  149. if err != nil {
  150. log.Error("HTMLString: %v", err)
  151. return
  152. }
  153. msg := NewMessage([]string{u.Email()}, c.Tr("mail.register_notify"), body)
  154. msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID())
  155. Send(msg)
  156. }
  157. // SendCollaboratorMail sends mail notification to new collaborator.
  158. func SendCollaboratorMail(u, doer User, repo Repository) {
  159. subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repo.FullName())
  160. data := map[string]any{
  161. "Subject": subject,
  162. "RepoName": repo.FullName(),
  163. "Link": repo.HTMLURL(),
  164. }
  165. body, err := render(MAIL_NOTIFY_COLLABORATOR, data)
  166. if err != nil {
  167. log.Error("HTMLString: %v", err)
  168. return
  169. }
  170. msg := NewMessage([]string{u.Email()}, subject, body)
  171. msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID())
  172. Send(msg)
  173. }
  174. func composeTplData(subject, body, link string) map[string]any {
  175. data := make(map[string]any, 10)
  176. data["Subject"] = subject
  177. data["Body"] = body
  178. data["Link"] = link
  179. return data
  180. }
  181. func composeIssueMessage(issue Issue, repo Repository, doer User, tplName string, tos []string, info string) *Message {
  182. subject := issue.MailSubject()
  183. body := string(markup.Markdown([]byte(issue.Content()), repo.HTMLURL(), repo.ComposeMetas()))
  184. data := composeTplData(subject, body, issue.HTMLURL())
  185. data["Doer"] = doer
  186. content, err := render(tplName, data)
  187. if err != nil {
  188. log.Error("HTMLString (%s): %v", tplName, err)
  189. }
  190. from := gomail.NewMessage().FormatAddress(conf.Email.FromEmail.Address, doer.DisplayName())
  191. msg := NewMessageFrom(tos, from, subject, content)
  192. msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
  193. return msg
  194. }
  195. // SendIssueCommentMail composes and sends issue comment emails to target receivers.
  196. func SendIssueCommentMail(issue Issue, repo Repository, doer User, tos []string) {
  197. if len(tos) == 0 {
  198. return
  199. }
  200. Send(composeIssueMessage(issue, repo, doer, MAIL_ISSUE_COMMENT, tos, "issue comment"))
  201. }
  202. // SendIssueMentionMail composes and sends issue mention emails to target receivers.
  203. func SendIssueMentionMail(issue Issue, repo Repository, doer User, tos []string) {
  204. if len(tos) == 0 {
  205. return
  206. }
  207. Send(composeIssueMessage(issue, repo, doer, MAIL_ISSUE_MENTION, tos, "issue mention"))
  208. }