package ldap import ( "github.com/astaxie/beego/logs" "nginx-ui/server/models" "nginx-ui/server/modules/mail" "nginx-ui/server/modules/settings" "strings" ) type Replace struct { Old string New string } func CreateReplace(o string, n string) *Replace { return &Replace{ Old: o, New: n, } } // 发送邮件 // 1、注册账号,发送初始化邮件 // 2、忘记密码,发送邮件 var ConfigUserAddSend = "LDAP_USER_EMAIL_ADD_SEND" func SendUserAddEmail(user *models.LdapUser, password string) { list := settings.GetSettings(ConfigUserAddSend) if len(list) == 0 { logs.Warn("not config add user email!") return } if user.Mail == "" { logs.Warn("user's mail is empty") return } content := "" subject := "" for _, c := range list { if strings.HasSuffix(c.ConfigKey, "@Content") { content = c.ConfigValue } else if strings.HasSuffix(c.ConfigKey, "@Subject") { subject = c.ConfigValue } } if subject == "" || content == "" { logs.Warn("subject or content is empty") return } replaces := []*Replace{ CreateReplace("{{username}}", user.UserName), CreateReplace("{{password}}", password), CreateReplace("{{account}}", user.Account), } for _, replace := range replaces { content = strings.ReplaceAll(content, replace.Old, replace.New) } _ = mail.SendEmail(user.Mail, subject, content, nil) }