email.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package utils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/http"
  6. "gopkg.in/gomail.v2"
  7. "mime"
  8. "strings"
  9. )
  10. var (
  11. AlarmMsgUrl = "http://127.0.0.1:8606/api/alarm/send"
  12. )
  13. // 发送邮件
  14. func SendEmail(title, content string, touser string) bool {
  15. if RunMode == "debug" {
  16. return false
  17. }
  18. var arr []string
  19. sub := strings.Index(touser, ";")
  20. if sub >= 0 {
  21. spArr := strings.Split(touser, ";")
  22. for _, v := range spArr {
  23. arr = append(arr, v)
  24. }
  25. } else {
  26. arr = append(arr, touser)
  27. }
  28. m := gomail.NewMessage()
  29. m.SetHeader("From", "317699326@qq.com ")
  30. m.SetHeader("To", arr...)
  31. m.SetHeader("Subject", title+" "+GetRandString(16))
  32. m.SetBody("text/html", content)
  33. d := gomail.NewDialer("smtp.qq.com", 587, "317699326@qq.com", "oqdypwfcvruwcbea")
  34. if err := d.DialAndSend(m); err != nil {
  35. return false
  36. }
  37. return true
  38. }
  39. // projectName-项目名称
  40. // runMode-运行模式
  41. // msgBody-消息内容
  42. // level:消息基本,1:提示消息,2:警告消息,3:严重错误信息,默认为1 提示消息
  43. func SendAlarmMsg(msgBody string, level int) {
  44. params := make(map[string]interface{})
  45. params["ProjectName"] = APPNAME
  46. params["RunMode"] = RunMode
  47. params["MsgBody"] = msgBody
  48. params["Level"] = level
  49. param, err := json.Marshal(params)
  50. if err != nil {
  51. FileLog.Info("SendAlarmMsg json.Marshal Err:" + err.Error())
  52. return
  53. }
  54. http.Post(AlarmMsgUrl, string(param))
  55. }
  56. // 发送邮件
  57. func SendEmailByHz(title, content string, touser string) (result bool, err error) {
  58. //if RunMode == "debug" {
  59. // result = false
  60. // return result, err
  61. //}
  62. var arr []string
  63. sub := strings.Index(touser, ";")
  64. if sub >= 0 {
  65. spArr := strings.Split(touser, ";")
  66. for _, v := range spArr {
  67. arr = append(arr, v)
  68. }
  69. } else {
  70. arr = append(arr, touser)
  71. }
  72. m := gomail.NewMessage()
  73. m.SetHeader("From", "hzcygx@hzinsights.com")
  74. m.SetHeader("To", arr...)
  75. m.SetHeader("Subject", title)
  76. m.SetBody("text/html", content)
  77. d := gomail.NewDialer("smtp.mxhichina.com", 465, "hzcygx@hzinsights.com", "Hzinsights2024")
  78. if err := d.DialAndSend(m); err != nil {
  79. result = false
  80. return result, err
  81. }
  82. result = true
  83. return
  84. }
  85. // 发送带有文件的邮件
  86. func SendEmailHaveFile(title, content string, fileName, touser string) bool {
  87. var arr []string
  88. sub := strings.Index(touser, ";")
  89. if sub >= 0 {
  90. spArr := strings.Split(touser, ";")
  91. for _, v := range spArr {
  92. arr = append(arr, v)
  93. }
  94. } else {
  95. arr = append(arr, touser)
  96. }
  97. m := gomail.NewMessage()
  98. m.SetHeader("From", "317699326@qq.com ")
  99. m.SetHeader("To", arr...)
  100. m.SetHeader("Subject", title)
  101. m.Attach(fileName)
  102. m.SetBody("text/html", content)
  103. d := gomail.NewDialer("smtp.qq.com", 587, "317699326@qq.com", "oqdypwfcvruwcbea")
  104. if err := d.DialAndSend(m); err != nil {
  105. return false
  106. }
  107. return true
  108. }
  109. // 发送邮件
  110. func SendEmailByHongze(title, content string, touser, attachPath, attachName string) bool {
  111. var arr []string
  112. sub := strings.Index(touser, ";")
  113. if sub >= 0 {
  114. spArr := strings.Split(touser, ";")
  115. for _, v := range spArr {
  116. arr = append(arr, v)
  117. }
  118. } else {
  119. arr = append(arr, touser)
  120. }
  121. m := gomail.NewMessage()
  122. m.SetHeader("From", "hzcygx@hzinsights.com")
  123. m.SetHeader("To", arr...)
  124. m.SetHeader("Subject", title)
  125. m.SetBody("text/html", content)
  126. //body := new(bytes.Buffer)
  127. if attachPath != "" {
  128. m.Attach(attachPath,
  129. gomail.Rename(attachName),
  130. gomail.SetHeader(map[string][]string{
  131. "Content-Disposition": []string{
  132. fmt.Sprintf(`attachment; filename="%s"`, mime.QEncoding.Encode("UTF-8", attachName)),
  133. },
  134. }))
  135. }
  136. d := gomail.NewDialer("smtp.mxhichina.com", 465, "hzcygx@hzinsights.com", "Hzinsights2024")
  137. if err := d.DialAndSend(m); err != nil {
  138. fmt.Println("send err:", err.Error())
  139. return false
  140. }
  141. return true
  142. }