123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package user
- import (
- "errors"
- "github.com/astaxie/beego/logs"
- "nginx-ui/server/base"
- "nginx-ui/server/models"
- "nginx-ui/server/modules/mail"
- "nginx-ui/server/modules/settings"
- "strings"
- )
- var ConfigUserResetPass = "USER_EMAIL_RESET_PASS"
- // SendUserResetPassEmail 忘记密码邮件,to: 邮件地址, link: 密码或者链接
- func SendUserResetPassEmail(user *models.User, link string) error {
- list := settings.GetSettings(ConfigUserResetPass)
- if len(list) == 0 {
- logs.Warn("not config add user email!")
- return errors.New("系统未配置邮件信息")
- }
- if user.Email == "" {
- logs.Warn("user's mail is empty")
- return errors.New("账号未绑定邮箱地址!")
- }
- 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 errors.New("系统未正确配置邮件信息!")
- }
- replaces := []*base.Replace{
- base.CreateReplace("{{username}}", user.Nickname),
- base.CreateReplace("{{account}}", user.Account),
- base.CreateReplace("{{link}}", link),
- base.CreateReplace("{{password}}", link),
- }
- for _, replace := range replaces {
- content = strings.ReplaceAll(content, replace.Old, replace.New)
- }
- err := mail.SendEmail(user.Email, subject, content, nil)
- if err != nil {
- return errors.New("邮件发送失败,请稍后重试!")
- }
- return nil
- }
|