mail.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2014 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. "encoding/hex"
  7. "fmt"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/log"
  11. "github.com/gogits/gogs/modules/middleware"
  12. )
  13. // Create New mail message use MailFrom and MailUser
  14. func NewMailMessage(To []string, subject, body string) Message {
  15. msg := NewHtmlMessage(To, base.MailService.User, subject, body)
  16. msg.User = base.MailService.User
  17. return msg
  18. }
  19. func GetMailTmplData(user *models.User) map[interface{}]interface{} {
  20. data := make(map[interface{}]interface{}, 10)
  21. data["AppName"] = base.AppName
  22. data["AppVer"] = base.AppVer
  23. data["AppUrl"] = base.AppUrl
  24. data["AppLogo"] = base.AppLogo
  25. data["ActiveCodeLives"] = base.Service.ActiveCodeLives / 60
  26. data["ResetPwdCodeLives"] = base.Service.ResetPwdCodeLives / 60
  27. if user != nil {
  28. data["User"] = user
  29. }
  30. return data
  31. }
  32. // create a time limit code for user active
  33. func CreateUserActiveCode(user *models.User, startInf interface{}) string {
  34. hours := base.Service.ActiveCodeLives / 60
  35. data := base.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands
  36. code := base.CreateTimeLimitCode(data, hours, startInf)
  37. // add tail hex username
  38. code += hex.EncodeToString([]byte(user.LowerName))
  39. return code
  40. }
  41. // Send user register mail with active code
  42. func SendRegisterMail(r *middleware.Render, user *models.User) {
  43. code := CreateUserActiveCode(user, nil)
  44. subject := "Register success, Welcome"
  45. data := GetMailTmplData(user)
  46. data["Code"] = code
  47. body, err := r.HTMLString("mail/auth/register_success", data)
  48. if err != nil {
  49. log.Error("mail.SendRegisterMail(fail to render): %v", err)
  50. return
  51. }
  52. msg := NewMailMessage([]string{user.Email}, subject, body)
  53. msg.Info = fmt.Sprintf("UID: %d, send register mail", user.Id)
  54. // async send mail
  55. SendAsync(msg)
  56. }
  57. // Send email verify active email.
  58. func SendActiveMail(r *middleware.Render, user *models.User) {
  59. code := CreateUserActiveCode(user, nil)
  60. subject := "Verify your email address"
  61. data := GetMailTmplData(user)
  62. data["Code"] = code
  63. body, err := r.HTMLString("mail/auth/active_email.html", data)
  64. if err != nil {
  65. log.Error("mail.SendActiveMail(fail to render): %v", err)
  66. return
  67. }
  68. msg := NewMailMessage([]string{user.Email}, subject, body)
  69. msg.Info = fmt.Sprintf("UID: %d, send email verify mail", user.Id)
  70. // async send mail
  71. SendAsync(msg)
  72. }