email.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package utils
  2. import (
  3. "fmt"
  4. "gopkg.in/gomail.v2"
  5. "strings"
  6. )
  7. var (
  8. emails = []struct {
  9. address string
  10. authCode string
  11. }{
  12. {
  13. address: "qunao007@qq.com",
  14. authCode: "cyhjgffhaobteihd",
  15. },
  16. {
  17. address: "2370889154@qq.com",
  18. authCode: "pfujkfwyqaprdjgb",
  19. },
  20. }
  21. )
  22. //发送邮件
  23. func SendEmail(title, content string, touser string) bool {
  24. var arr []string
  25. sub := strings.Index(touser, ";")
  26. if sub >= 0 {
  27. spArr := strings.Split(touser, ";")
  28. for _, v := range spArr {
  29. arr = append(arr, v)
  30. }
  31. } else {
  32. arr = append(arr, touser)
  33. }
  34. rand := GetRandInt(1, len(emails)+1)
  35. emailObj := emails[rand-1]
  36. fmt.Println(emailObj.address, emailObj.authCode)
  37. emailAddress := emailObj.address
  38. emailAuthCode := emailObj.authCode
  39. m := gomail.NewMessage()
  40. m.SetHeader("From", emailAddress)
  41. m.SetHeader("To", arr...)
  42. m.SetHeader("Subject", title+" "+GetRandString(16))
  43. m.SetBody("text/html", content)
  44. d := gomail.NewDialer("smtp.qq.com", 587, emailAddress, emailAuthCode)
  45. if err := d.DialAndSend(m); err != nil {
  46. fmt.Println("DialAndSend Err:" + err.Error())
  47. return false
  48. }
  49. return true
  50. }