mail.go 5.6 KB

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