package services import ( "crypto/tls" "eta/eta_api/models" "eta/eta_api/utils" "fmt" "gopkg.in/gomail.v2" "strconv" "strings" ) func SendEmailToCompany() { // 获取收件人列表 //emailCond := " AND enabled = 1 " emailCond := "" emailPars := make([]interface{}, 0) emails, e := models.GetEnglishReportEmailList(emailCond, emailPars, "") if e != nil { fmt.Println("获取收件人列表失败, Err: " + e.Error()) return } if len(emails) == 0 { fmt.Println("收件人列表为空") return } // TODO:这是HTML模板内容 template := ` Horizon Insights x Fastmarkets x FGE: 2024 Macro & Commodities Outlook - Registration Open!
2024 Macro & Commodities Outlook Invite

Dear Client,

Happy Year-End Holidays!

As we usher in the new year, Horizon Insights, in collaboration with Fastmarkets and FGE, is proud to announce our 2024 Macro & Commodities Outlook which will be held virtually on 10 & 11 Jan 20245pm - 8pm SGT (i.e. 9am - 12pm BST). Our experienced analysts from Horizon Insights together with guest speakers from our valued partners will present our views in the fields of Macro, Ferrous Metals, Base Metals, Energy, as well as Petrochemicals.

Please see below for our event schedule:

  
   


To register, simply scan the QR code in the posters above to indicate your interest. Alternatively, you may also register here.
Should you encounter any difficulties registering, please contact Stephanie (stephanie@hzinsights.com).
The virtual meeting details will be disseminated to you a few days prior to the event.

Lastly, our firm would like to wish all of you a prosperous Happy New Year! We look forward to seeing everyone on the 10th & 11th.

Cheers!







` // 推送信息 sendData := make([]*EnglishReportSendEmailRequest, 0) for i := range emails { r := new(EnglishReportSendEmailRequest) r.EmailId = emails[i].Id r.Email = strings.Replace(emails[i].Email, " ", "", -1) r.Subject = "Horizon Insights x Fastmarkets x FGE: 2024 Macro & Commodities Outlook - Registration Open!" // TODO:这是主题 r.FromAlias = "Horizon FICC" // TODO:这是推送人(中文) r.HtmlBody = template sendData = append(sendData, r) } if len(sendData) == 0 { fmt.Println("无邮件可推送") return } // 请求阿里云接口批量推送 aliEmail := new(AliyunEmail) resultList, e := aliEmail.BatchSendEmail(sendData) if e != nil { fmt.Println("批量推送失败, Err: " + e.Error()) return } for _, r := range resultList { utils.FileLog.Info("email: %s, ok: %v, res: %s", r.Email, r.Ok, r.ResultData) if r.Ok { fmt.Println("发送成功") } else { fmt.Println("发送失败:" + r.ResultData) } } } type SendEmailReq struct { Title string `description:"标题"` Content string `description:"内容"` ToUser []string `description:"收信人邮箱"` } func SendEmail(req SendEmailReq) (success bool, err error) { if req.Title == "" { err = fmt.Errorf("邮件主题不可为空") return } if req.Content == "" { err = fmt.Errorf("邮件内容不可为空") return } if len(req.ToUser) <= 0 { err = fmt.Errorf("收信人不可为空") return } // 邮箱配置 confMap, e := models.GetBusinessConf() if e != nil { err = fmt.Errorf("GetBusinessConf err: %s", e.Error()) return } checkArr := []string{ models.BusinessConfEmailServerHost, models.BusinessConfEmailServerPort, models.BusinessConfEmailSender, models.BusinessConfEmailSenderUserName, models.BusinessConfEmailSenderPassword, } for _, v := range checkArr { if confMap[v] == "" { err = fmt.Errorf("%s配置有误", v) return } } port, _ := strconv.Atoi(confMap[models.BusinessConfEmailServerPort]) if port <= 0 { port = 587 // 默认587端口 } m := gomail.NewMessage() m.SetHeader("From", confMap[models.BusinessConfEmailSender]) m.SetHeader("To", req.ToUser...) m.SetHeader("Subject", req.Title) m.SetBody("text/html", req.Content) d := gomail.NewDialer(confMap[models.BusinessConfEmailServerHost], port, confMap[models.BusinessConfEmailSenderUserName], confMap[models.BusinessConfEmailSenderPassword]) // 解决x509报错的问题。证书不通过。跳过证书验证 config := &tls.Config{ServerName: confMap[models.BusinessConfEmailServerHost], InsecureSkipVerify: true} d.TLSConfig = config if e = d.DialAndSend(m); e != nil { err = fmt.Errorf("邮件发送失败, Err: %s", e.Error()) return } success = true return }