email.go 1.4 KB

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