email.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package utils
  2. import (
  3. "crypto/tls"
  4. "strings"
  5. "gopkg.in/gomail.v2"
  6. )
  7. // 发送邮件
  8. func SendEmail(title, content string, touser string) bool {
  9. var arr []string
  10. sub := strings.Index(touser, ";")
  11. if sub >= 0 {
  12. spArr := strings.Split(touser, ";")
  13. for _, v := range spArr {
  14. arr = append(arr, v)
  15. }
  16. } else {
  17. arr = append(arr, touser)
  18. }
  19. m := gomail.NewMessage()
  20. m.SetHeader("From", "")
  21. m.SetHeader("To", arr...)
  22. m.SetHeader("Subject", title+" "+GetRandString(16))
  23. m.SetBody("text/html", content)
  24. d := gomail.NewDialer("smtp.qq.com", 587, "", "")
  25. if err := d.DialAndSend(m); err != nil {
  26. return false
  27. }
  28. return true
  29. }
  30. // 发送邮件
  31. func SendEmailByDw(title, content string, touser string) (result bool, err error) {
  32. var arr []string
  33. sub := strings.Index(touser, ";")
  34. if sub >= 0 {
  35. spArr := strings.Split(touser, ";")
  36. arr = append(arr, spArr...)
  37. // for _, v := range spArr {
  38. // arr = append(arr, v)
  39. // }
  40. } else {
  41. arr = append(arr, touser)
  42. }
  43. m := gomail.NewMessage()
  44. if RunMode == "debug" {
  45. m.SetHeader("From", "564693862@qq.com")
  46. } else {
  47. m.SetHeader("From", "lvan@dwqh88.com")
  48. }
  49. m.SetHeader("To", arr...)
  50. m.SetHeader("Subject", title)
  51. m.SetBody("text/html", content)
  52. var d *gomail.Dialer
  53. if RunMode == "debug" {
  54. d = gomail.NewDialer("smtp.qq.com", 587, "564693862@qq.com", "xvjmiuepszmmbdhc")
  55. } else {
  56. d = gomail.NewDialer("smtp.dwqh88.com", 25, "lvan@dwqh88.com", "Dwqh20248888")
  57. }
  58. // d := gomail.NewDialer("mail.dwqh88.com", 465, "lvan@dwqh88.com", "Dwqh20248888")
  59. // d := gomail.NewDialer("smtp.qq.com", 587, "564693862@qq.com", "xvjmiuepszmmbdhc")
  60. d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
  61. if err := d.DialAndSend(m); err != nil {
  62. result = false
  63. return result, err
  64. }
  65. result = true
  66. return
  67. }