(create dto.AlertLogCreate, alert dto.AlertDTO, params []dto.Param, transport *http.Transport, agentInfo *dto.AgentInfo, emailConfig model.AlertConfig)
| 40 | } |
| 41 | |
| 42 | func CreateEmailAlertLog(create dto.AlertLogCreate, alert dto.AlertDTO, params []dto.Param, transport *http.Transport, agentInfo *dto.AgentInfo, emailConfig model.AlertConfig) error { |
| 43 | var alertLog model.AlertLog |
| 44 | alertRepo := repo.NewIAlertRepo() |
| 45 | config, err := alertRepo.GetConfig(alertRepo.WithByType(constant.CommonConfig)) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | var cfg dto.AlertCommonConfig |
| 50 | err = json.Unmarshal([]byte(config.Config), &cfg) |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | var emailInfo dto.AlertEmailConfig |
| 55 | err = json.Unmarshal([]byte(emailConfig.Config), &emailInfo) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | if emailInfo.Host == "" { |
| 60 | create.Message = "email config is required" |
| 61 | create.Status = constant.AlertError |
| 62 | return SaveAlertLog(create, &alertLog) |
| 63 | } |
| 64 | if !global.IsMaster && cfg.IsOffline == constant.StatusEnable { |
| 65 | create.Status = constant.AlertPushing |
| 66 | return SaveAlertLog(create, &alertLog) |
| 67 | } else { |
| 68 | username := emailInfo.UserName |
| 69 | if username == "" { |
| 70 | username = emailInfo.Sender |
| 71 | } |
| 72 | encodedDisplayName := mime.BEncoding.Encode("UTF-8", emailInfo.DisplayName) |
| 73 | smtpConfig := email.SMTPConfig{ |
| 74 | Host: emailInfo.Host, |
| 75 | Port: emailInfo.Port, |
| 76 | Sender: emailInfo.Sender, |
| 77 | Username: username, |
| 78 | Password: emailInfo.Password, |
| 79 | From: fmt.Sprintf(`"%s" <%s>`, encodedDisplayName, emailInfo.Sender), |
| 80 | Encryption: emailInfo.Encryption, |
| 81 | Recipient: emailInfo.Recipient, |
| 82 | } |
| 83 | content := GetSendContent(alert.Type, params, agentInfo) |
| 84 | if content == "" { |
| 85 | content = i18n.GetMsgWithMap("CommonAlert", map[string]interface{}{"msg": alert.Title}) |
| 86 | } |
| 87 | msg := email.EmailMessage{ |
| 88 | Subject: i18n.GetMsgByKey("PanelAlertTitle"), |
| 89 | Body: content, |
| 90 | IsHTML: true, |
| 91 | } |
| 92 | |
| 93 | if err = email.SendMail(smtpConfig, msg, transport); err != nil { |
| 94 | create.Message = err.Error() |
| 95 | create.Status = constant.AlertError |
| 96 | return SaveAlertLog(create, &alertLog) |
| 97 | } |
| 98 | create.Status = constant.AlertSuccess |
| 99 | return SaveAlertLog(create, &alertLog) |
no test coverage detected