mail.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package ldap
  2. import (
  3. "github.com/astaxie/beego/logs"
  4. "nginx-ui/server/models"
  5. "nginx-ui/server/modules/mail"
  6. "nginx-ui/server/modules/settings"
  7. "strings"
  8. )
  9. type Replace struct {
  10. Old string
  11. New string
  12. }
  13. func CreateReplace(o string, n string) *Replace {
  14. return &Replace{
  15. Old: o,
  16. New: n,
  17. }
  18. }
  19. // 发送邮件
  20. // 1、注册账号,发送初始化邮件
  21. // 2、忘记密码,发送邮件
  22. var ConfigUserAddSend = "LDAP_USER_EMAIL_ADD_SEND"
  23. func SendUserAddEmail(user *models.LdapUser, password string) {
  24. list := settings.GetSettings(ConfigUserAddSend)
  25. if len(list) == 0 {
  26. logs.Warn("not config add user email!")
  27. return
  28. }
  29. if user.Mail == "" {
  30. logs.Warn("user's mail is empty")
  31. return
  32. }
  33. content := ""
  34. subject := ""
  35. for _, c := range list {
  36. if strings.HasSuffix(c.ConfigKey, "@Content") {
  37. content = c.ConfigValue
  38. } else if strings.HasSuffix(c.ConfigKey, "@Subject") {
  39. subject = c.ConfigValue
  40. }
  41. }
  42. if subject == "" || content == "" {
  43. logs.Warn("subject or content is empty")
  44. return
  45. }
  46. replaces := []*Replace{
  47. CreateReplace("{{username}}", user.UserName),
  48. CreateReplace("{{password}}", password),
  49. CreateReplace("{{account}}", user.Account),
  50. }
  51. for _, replace := range replaces {
  52. content = strings.ReplaceAll(content, replace.Old, replace.New)
  53. }
  54. _ = mail.SendEmail(user.Mail, subject, content, nil)
  55. }