email.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package email
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "go-common/app/job/main/videoup-report/model/email"
  9. "go-common/library/log"
  10. "go-common/library/queue/databus/report"
  11. gomail "gopkg.in/gomail.v2"
  12. )
  13. //SendMail send the email
  14. func (d *Dao) SendMail(tpl *email.Template) {
  15. var err error
  16. headers := tpl.Headers
  17. if len(headers[email.FROM]) == 0 || len(headers[email.TO]) == 0 || len(headers[email.SUBJECT]) == 0 {
  18. log.Error("email lack From/To/Subject: emailTemplate(%+v)", *tpl)
  19. return
  20. }
  21. if len(tpl.ContentType) == 0 {
  22. tpl.ContentType = "text/plain"
  23. }
  24. log.Info("start send mail: emailTemplate(%+v)", *tpl)
  25. msg := gomail.NewMessage()
  26. msg.SetHeaders(headers)
  27. msg.SetBody(tpl.ContentType, tpl.Body)
  28. result := email.EmailResOK
  29. if err = d.email.DialAndSend(msg); err != nil {
  30. result = email.EmailResFail
  31. log.Error("s.email.DialAndSend error(%v) emailTemplate(%+v)", err, tpl)
  32. }
  33. d.sendEmailLog(tpl, headers[email.TO], headers[email.CC], result)
  34. //retry
  35. if err != nil {
  36. address := headers[email.TO]
  37. if len(headers[email.CC]) > 0 {
  38. address = append(address, headers[email.CC]...)
  39. msg.SetHeader(email.CC)
  40. }
  41. for _, addr := range address {
  42. msg.SetHeader(email.TO, addr)
  43. result = email.EmailResOK
  44. if err = d.email.DialAndSend(msg); err != nil {
  45. result = email.EmailResFail
  46. log.Error("s.email.DialAndSend error(%v) to(%s) emailTemplate(%+v)", err, addr, tpl)
  47. }
  48. d.sendEmailLog(tpl, []string{addr}, []string{}, result)
  49. time.Sleep(time.Second * 5)
  50. }
  51. }
  52. }
  53. func (d *Dao) sendEmailLog(tpl *email.Template, to []string, cc []string, result string) {
  54. if tpl == nil || len(tpl.Headers) <= 0 || len(tpl.Headers[email.SUBJECT]) <= 0 {
  55. log.Error("sendEmailLog tpl nil | no headers, tpl(%+v)", tpl)
  56. return
  57. }
  58. address := fmt.Sprintf("to: %s", strings.Join(to, ","))
  59. if len(cc) > 0 {
  60. address = fmt.Sprintf("%s\ncc: %s", address, strings.Join(cc, ","))
  61. }
  62. item := &report.ManagerInfo{
  63. Uname: tpl.Username,
  64. UID: tpl.UID,
  65. Business: email.LogBusEmail,
  66. Type: email.LogTypeEmailJob,
  67. Oid: tpl.AID,
  68. Action: tpl.Type,
  69. Ctime: time.Now(),
  70. Content: map[string]interface{}{
  71. "subject": tpl.Headers[email.SUBJECT][0],
  72. "body": tpl.Body,
  73. "address": address,
  74. "department": tpl.Department,
  75. "result": result,
  76. },
  77. }
  78. report.Manager(item)
  79. log.Info("sendEmailLog template(%+v) result(%s) log.content(%+v)", tpl, result, item.Content)
  80. }
  81. //PushToRedis start to push email to redis according to speed
  82. func (d *Dao) PushToRedis(c context.Context, tpl *email.Template) (isFast bool, key string, err error) {
  83. if tpl == nil {
  84. return
  85. }
  86. //探查发邮件速度快慢
  87. isFast = d.detector.Detect(tpl.UID)
  88. //超限名单只能被回落或下一次超限名单替代
  89. if d.detector.IsFastUnique(tpl.UID) {
  90. key = email.MailFastKey
  91. d.fastChan <- 1
  92. } else {
  93. key = email.MailKey
  94. }
  95. if err = d.PushRedis(c, tpl, key); err != nil {
  96. log.Error("PushToRedis d.PushRedis error(%v) key(%s), tpl(%+v) ", err, key, tpl)
  97. }
  98. return
  99. }
  100. //Start get email from redis and send
  101. func (d *Dao) Start(key string) (err error) {
  102. var (
  103. bs []byte
  104. tpl = &email.Template{}
  105. )
  106. bs, err = d.PopRedis(context.TODO(), key)
  107. if err != nil || bs == nil {
  108. time.Sleep(5 * time.Second)
  109. return
  110. }
  111. err = json.Unmarshal(bs, tpl)
  112. if err != nil {
  113. log.Error("email Start json.unmarshal error(%v) template(%s)", err, string(bs))
  114. return
  115. }
  116. //控制邮件发送频率
  117. st := <-d.controlChan
  118. d.SendMail(tpl)
  119. time.Sleep(time.Second * 5)
  120. d.controlChan <- st
  121. return
  122. }