mail.go 5.8 KB

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