email.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package services
  2. import (
  3. "fmt"
  4. "gopkg.in/gomail.v2"
  5. "hongze/hongze_yb/global"
  6. "hongze/hongze_yb/utils"
  7. "strings"
  8. )
  9. //发送邮件
  10. func SendEmail(title, content string, touser string) bool {
  11. if global.CONFIG.Serve.RunMode == "debug" {
  12. global.LOG.Info(fmt.Sprint(title, ";", content))
  13. return false
  14. }
  15. var arr []string
  16. sub := strings.Index(touser, ";")
  17. if sub >= 0 {
  18. spArr := strings.Split(touser, ";")
  19. for _, v := range spArr {
  20. arr = append(arr, v)
  21. }
  22. } else {
  23. arr = append(arr, touser)
  24. }
  25. m := gomail.NewMessage()
  26. m.SetHeader("From", "317699326@qq.com ")
  27. m.SetHeader("To", arr...)
  28. m.SetHeader("Subject", title+" "+utils.GetRandString(16))
  29. m.SetBody("text/html", content)
  30. d := gomail.NewDialer("smtp.qq.com", 587, "317699326@qq.com", "oqdypwfcvruwcbea")
  31. if err := d.DialAndSend(m); err != nil {
  32. return false
  33. }
  34. return true
  35. }
  36. //发送邮件
  37. func SendEmailByHz(title, content string, touser string) (result bool, err error) {
  38. var arr []string
  39. sub := strings.Index(touser, ";")
  40. if sub >= 0 {
  41. spArr := strings.Split(touser, ";")
  42. for _, v := range spArr {
  43. arr = append(arr, v)
  44. }
  45. } else {
  46. arr = append(arr, touser)
  47. }
  48. m := gomail.NewMessage()
  49. m.SetHeader("From", "public@hzinsights.com")
  50. m.SetHeader("To", arr...)
  51. m.SetHeader("Subject", title)
  52. m.SetBody("text/html", content)
  53. d := gomail.NewDialer("smtp.mxhichina.com", 465, "public@hzinsights.com", "Hzinsights2018")
  54. if err := d.DialAndSend(m); err != nil {
  55. result = false
  56. return result, err
  57. }
  58. result = true
  59. return
  60. }