sendmain.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "os"
  8. qu "qfw/util"
  9. "qfw/util/mail"
  10. )
  11. var tomail string
  12. var api string
  13. var from,to,cc, smtpHost,user,pwd string
  14. var smtpPort int
  15. //api模式 二选一皆可
  16. func sendErrMailApi(title,body string) {
  17. jkmail, _ := sysconfig["jkmail"].(map[string]interface{})
  18. if jkmail != nil {
  19. tomail, _ = jkmail["to"].(string)
  20. api, _ = jkmail["api"].(string)
  21. }
  22. log.Println(tomail,api)
  23. res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", api, tomail, title, body))
  24. if err == nil {
  25. defer res.Body.Close()
  26. read, err := ioutil.ReadAll(res.Body)
  27. log.Println("邮件发送成功:", string(read), err)
  28. }else {
  29. log.Println("邮件发送失败:", err)
  30. }
  31. }
  32. func sendErrMailSmtp(title,body,xlsxName string) {
  33. smtpMail, _ := sysconfig["smtpMail"].(map[string]interface{})
  34. if smtpMail != nil {
  35. from, _ = smtpMail["from"].(string)
  36. to, _ = smtpMail["to"].(string)
  37. cc, _ = smtpMail["cc"].(string)
  38. smtpHost, _ = smtpMail["smtpHost"].(string)
  39. smtpPort= qu.IntAll(smtpMail["smtpPort"])
  40. user, _ = smtpMail["user"].(string)
  41. pwd, _ = smtpMail["pwd"].(string)
  42. }
  43. f, _ := os.Open(xlsxName)
  44. b, err := ioutil.ReadAll(f)
  45. if err != nil {
  46. fmt.Println("err:",err)
  47. return
  48. }
  49. ok := mail.GSendMail_Bq(from, to, cc, cc, title, body, f.Name(), b, &mail.GmailAuth{
  50. SmtpHost: smtpHost,
  51. SmtpPort: smtpPort,
  52. User: user,
  53. Pwd: pwd,
  54. })
  55. fmt.Println(ok)
  56. if !ok {//邮件发送异常
  57. sendWarningSmtp("邮件附件发送异常","请检查......")
  58. }
  59. }
  60. func sendWarningSmtp(title,body string) {
  61. smtpMail, _ := sysconfig["smtpMail"].(map[string]interface{})
  62. if smtpMail != nil {
  63. from, _ = smtpMail["from"].(string)
  64. to, _ = smtpMail["to"].(string)
  65. cc, _ = smtpMail["cc"].(string)
  66. smtpHost, _ = smtpMail["smtpHost"].(string)
  67. smtpPort= qu.IntAll(smtpMail["smtpPort"])
  68. user, _ = smtpMail["user"].(string)
  69. pwd, _ = smtpMail["pwd"].(string)
  70. }
  71. ok := mail.GSendMail(from, from, from, from, title, body, "", "", &mail.GmailAuth{
  72. SmtpHost: smtpHost,
  73. SmtpPort: smtpPort,
  74. User: user,
  75. Pwd: pwd,
  76. })
  77. fmt.Println(ok)
  78. }