email.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package email
  2. import (
  3. "fmt"
  4. "html/template"
  5. "net/mail"
  6. "path/filepath"
  7. "sync"
  8. "time"
  9. "github.com/cockroachdb/errors"
  10. "gopkg.in/macaron.v1"
  11. "gogs.io/gogs/internal/conf"
  12. "gogs.io/gogs/internal/markup"
  13. "gogs.io/gogs/templates"
  14. )
  15. const (
  16. tmplAuthActivate = "auth/activate"
  17. tmplAuthActivateEmail = "auth/activate_email"
  18. tmplAuthResetPassword = "auth/reset_passwd"
  19. tmplAuthRegisterNotify = "auth/register_notify"
  20. tmplIssueComment = "issue/comment"
  21. tmplIssueMention = "issue/mention"
  22. tmplNotifyCollaborator = "notify/collaborator"
  23. )
  24. var (
  25. tplRender *macaron.TplRender
  26. tplRenderOnce sync.Once
  27. )
  28. // render renders a mail template with given data.
  29. func render(tpl string, data map[string]any) (string, error) {
  30. tplRenderOnce.Do(func() {
  31. customDir := filepath.Join(conf.CustomDir(), "templates")
  32. opt := &macaron.RenderOptions{
  33. Directory: filepath.Join(conf.WorkDir(), "templates", "mail"),
  34. AppendDirectories: []string{filepath.Join(customDir, "mail")},
  35. Extensions: []string{".tmpl", ".html"},
  36. Funcs: []template.FuncMap{map[string]any{
  37. "AppName": func() string {
  38. return conf.App.BrandName
  39. },
  40. "AppURL": func() string {
  41. return conf.Server.ExternalURL
  42. },
  43. "Year": func() int {
  44. return time.Now().Year()
  45. },
  46. "Str2HTML": func(raw string) template.HTML {
  47. return template.HTML(markup.Sanitize(raw))
  48. },
  49. }},
  50. }
  51. if !conf.Server.LoadAssetsFromDisk {
  52. opt.TemplateFileSystem = templates.NewTemplateFileSystem("mail", customDir)
  53. }
  54. ts := macaron.NewTemplateSet()
  55. ts.Set(macaron.DEFAULT_TPL_SET_NAME, opt)
  56. tplRender = &macaron.TplRender{
  57. TemplateSet: ts,
  58. Opt: opt,
  59. }
  60. })
  61. return tplRender.HTMLString(tpl, data)
  62. }
  63. func SendTestMail(email string) error {
  64. msg, err := newMessage([]string{email}, "Gogs Test Email", "Hello 👋, greeting from Gogs!")
  65. if err != nil {
  66. return errors.Wrap(err, "new message")
  67. }
  68. return sendMessage(msg)
  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. GenerateEmailActivateCode(string) 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) error {
  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. return errors.Wrap(err, "render")
  99. }
  100. msg, err := newMessage([]string{u.Email()}, subject, body)
  101. if err != nil {
  102. return errors.Wrap(err, "new message")
  103. }
  104. msg.info = fmt.Sprintf("UID: %d, %s", u.ID(), info)
  105. send(msg)
  106. return nil
  107. }
  108. func SendActivateAccountMail(c *macaron.Context, u User) error {
  109. return SendUserMail(c, u, tmplAuthActivate, u.GenerateEmailActivateCode(u.Email()), c.Tr("mail.activate_account"), "activate account")
  110. }
  111. func SendResetPasswordMail(c *macaron.Context, u User) error {
  112. return SendUserMail(c, u, tmplAuthResetPassword, u.GenerateEmailActivateCode(u.Email()), c.Tr("mail.reset_password"), "reset password")
  113. }
  114. func SendActivateEmailMail(c *macaron.Context, u User, email string) error {
  115. data := map[string]any{
  116. "Username": u.DisplayName(),
  117. "ActiveCodeLives": conf.Auth.ActivateCodeLives / 60,
  118. "Code": u.GenerateEmailActivateCode(email),
  119. "Email": email,
  120. }
  121. body, err := render(tmplAuthActivateEmail, data)
  122. if err != nil {
  123. return errors.Wrap(err, "render")
  124. }
  125. msg, err := newMessage([]string{email}, c.Tr("mail.activate_email"), body)
  126. if err != nil {
  127. return errors.Wrap(err, "new message")
  128. }
  129. msg.info = fmt.Sprintf("UID: %d, activate email", u.ID())
  130. send(msg)
  131. return nil
  132. }
  133. func SendRegisterNotifyMail(c *macaron.Context, u User) error {
  134. data := map[string]any{
  135. "Username": u.DisplayName(),
  136. }
  137. body, err := render(tmplAuthRegisterNotify, data)
  138. if err != nil {
  139. return errors.Wrap(err, "render")
  140. }
  141. msg, err := newMessage([]string{u.Email()}, c.Tr("mail.register_notify"), body)
  142. if err != nil {
  143. return errors.Wrap(err, "new message")
  144. }
  145. msg.info = fmt.Sprintf("UID: %d, registration notify", u.ID())
  146. send(msg)
  147. return nil
  148. }
  149. func SendCollaboratorMail(u, doer User, repo Repository) error {
  150. subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repo.FullName())
  151. data := map[string]any{
  152. "Subject": subject,
  153. "RepoName": repo.FullName(),
  154. "Link": repo.HTMLURL(),
  155. }
  156. body, err := render(tmplNotifyCollaborator, data)
  157. if err != nil {
  158. return errors.Wrap(err, "render")
  159. }
  160. msg, err := newMessage([]string{u.Email()}, subject, body)
  161. if err != nil {
  162. return errors.Wrap(err, "new message")
  163. }
  164. msg.info = fmt.Sprintf("UID: %d, add collaborator", u.ID())
  165. send(msg)
  166. return nil
  167. }
  168. func composeTplData(subject, body, link string) map[string]any {
  169. data := make(map[string]any, 10)
  170. data["Subject"] = subject
  171. data["Body"] = body
  172. data["Link"] = link
  173. return data
  174. }
  175. func composeIssueMessage(issue Issue, repo Repository, doer User, tplName string, tos []string, info string) (*message, error) {
  176. subject := issue.MailSubject()
  177. body := string(markup.Markdown([]byte(issue.Content()), repo.HTMLURL(), repo.ComposeMetas()))
  178. data := composeTplData(subject, body, issue.HTMLURL())
  179. data["Doer"] = doer
  180. content, err := render(tplName, data)
  181. if err != nil {
  182. return nil, errors.Wrapf(err, "render %q", tplName)
  183. }
  184. from := (&mail.Address{Name: doer.DisplayName(), Address: conf.Email.FromEmail}).String()
  185. msg, err := newMessageFrom(tos, from, subject, content)
  186. if err != nil {
  187. return nil, errors.Wrap(err, "new message")
  188. }
  189. msg.info = fmt.Sprintf("Subject: %s, %s", subject, info)
  190. return msg, nil
  191. }
  192. // SendIssueCommentMail composes and sends issue comment emails to target receivers.
  193. func SendIssueCommentMail(issue Issue, repo Repository, doer User, tos []string) error {
  194. if len(tos) == 0 {
  195. return nil
  196. }
  197. msg, err := composeIssueMessage(issue, repo, doer, tmplIssueComment, tos, "issue comment")
  198. if err != nil {
  199. return errors.Wrap(err, "compose issue message")
  200. }
  201. send(msg)
  202. return nil
  203. }
  204. // SendIssueMentionMail composes and sends issue mention emails to target receivers.
  205. func SendIssueMentionMail(issue Issue, repo Repository, doer User, tos []string) error {
  206. if len(tos) == 0 {
  207. return nil
  208. }
  209. msg, err := composeIssueMessage(issue, repo, doer, tmplIssueMention, tos, "issue mention")
  210. if err != nil {
  211. return errors.Wrap(err, "compose issue message")
  212. }
  213. send(msg)
  214. return nil
  215. }