email.go 1.7 KB

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