email.go 972 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package utils
  2. import (
  3. "fmt"
  4. "gopkg.in/gomail.v2"
  5. "runtime"
  6. "strings"
  7. )
  8. func SendEmail(title, content string, touser string) bool {
  9. emailSource := ``
  10. pc, _, line, ok := runtime.Caller(1)
  11. if ok{
  12. emailSource = fmt.Sprint( runtime.FuncForPC(pc).Name(), ",第", line, "行:")
  13. }
  14. content = fmt.Sprint(emailSource,"\n",content)
  15. if RunMode == "debug" {
  16. FileLog.Info(fmt.Sprint(title, ";", content))
  17. return false
  18. }
  19. var arr []string
  20. sub := strings.Index(touser, ";")
  21. if sub >= 0 {
  22. spArr := strings.Split(touser, ";")
  23. for _, v := range spArr {
  24. arr = append(arr, v)
  25. }
  26. } else {
  27. arr = append(arr, touser)
  28. }
  29. m := gomail.NewMessage()
  30. m.SetHeader("From", "317699326@qq.com ")
  31. m.SetHeader("To", arr...)
  32. m.SetHeader("Subject", title+" "+GetRandString(16))
  33. m.SetBody("text/html", content)
  34. d := gomail.NewDialer("smtp.qq.com", 587, "317699326@qq.com", "oqdypwfcvruwcbea")
  35. if err := d.DialAndSend(m); err != nil {
  36. return false
  37. }
  38. return true
  39. }