email.go 986 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. }