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