email.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package services
  2. import (
  3. "gopkg.in/gomail.v2"
  4. "hongze/hongze_yb_en_api/services/alarm_msg"
  5. "strings"
  6. )
  7. // SendEmailCode 发送邮件
  8. func SendEmailCode(name, email string) (err error, errMsg string) {
  9. defer func() {
  10. if err != nil {
  11. go alarm_msg.SendAlarmMsg("阿里云群发英文研报邮件失败, Err: "+err.Error(), 3)
  12. }
  13. }()
  14. // 请求阿里云接口批量推送
  15. aliEmail := new(AliyunEmail)
  16. _, e := aliEmail.SendEmailCode(name, email)
  17. if e != nil {
  18. err = e
  19. return
  20. }
  21. return
  22. }
  23. //发送邮件
  24. func SendEmailByHz(title, content string, touser string) (result bool, err error) {
  25. var arr []string
  26. sub := strings.Index(touser, ";")
  27. if sub >= 0 {
  28. spArr := strings.Split(touser, ";")
  29. for _, v := range spArr {
  30. arr = append(arr, v)
  31. }
  32. } else {
  33. arr = append(arr, touser)
  34. }
  35. m := gomail.NewMessage()
  36. m.SetHeader("From", "public@hzinsights.com")
  37. m.SetHeader("To", arr...)
  38. m.SetHeader("Subject", title)
  39. m.SetBody("text/html", content)
  40. d := gomail.NewDialer("smtp.mxhichina.com", 465, "public@hzinsights.com", "Hzinsights2018")
  41. if err := d.DialAndSend(m); err != nil {
  42. result = false
  43. return result, err
  44. }
  45. result = true
  46. return
  47. }