aliyun_email.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package services
  2. import (
  3. "encoding/json"
  4. openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  5. dm "github.com/alibabacloud-go/dm-20151123/v2/client"
  6. util "github.com/alibabacloud-go/tea-utils/v2/service"
  7. "github.com/alibabacloud-go/tea/tea"
  8. "time"
  9. )
  10. var (
  11. AliyunEmailAccountName = "ficcemail@hzmail.hzinsights.com"
  12. AliyunEmailAccessKeyId = "LTAIFMZYQhS2BTvW"
  13. AliyunEmailAccessKeySecret = "12kk1ptCHoGWedhBnKRVW5hRJzq9Fq"
  14. AliyunEmailReplyAddress = "ficcemail@hzinsights.com"
  15. AliyunEmailReplyAddressAlias = "弘则研究"
  16. )
  17. // AliyunEmail 阿里云邮件
  18. type AliyunEmail struct {
  19. Client *dm.Client
  20. }
  21. // NewClient
  22. func (em *AliyunEmail) NewClient() (err error) {
  23. config := &openapi.Config{
  24. AccessKeyId: tea.String(AliyunEmailAccessKeyId),
  25. AccessKeySecret: tea.String(AliyunEmailAccessKeySecret),
  26. }
  27. // 访问的域名
  28. config.Endpoint = tea.String("dm.aliyuncs.com")
  29. _result, _err := dm.NewClient(config)
  30. em.Client = _result
  31. err = _err
  32. return
  33. }
  34. // AliyunEmailResult 邮件推送响应体
  35. type AliyunEmailResult struct {
  36. Code string `description:"错误码"`
  37. StatusCode int `description:"状态码"`
  38. Message string `description:"响应信息"`
  39. Data string `description:"请求体"`
  40. Description string `description:"描述"`
  41. EnvId string `description:"请求成功-事件ID"`
  42. RequestId string `description:"请求成功-请求ID"`
  43. }
  44. // AliyunEmailResultData 邮件推送响应内容
  45. type AliyunEmailResultData struct {
  46. Code string `description:"错误码"`
  47. HostId string `description:""`
  48. Message string `description:"响应信息"`
  49. Recommend string `description:""`
  50. RequestId string `description:"请求ID"`
  51. StatusCode int `description:"状态码"`
  52. }
  53. // SendEmail 邮件推送
  54. func (em *AliyunEmail) SendEmail(item *EnglishReportSendEmailRequest) (ok bool, result string, err error) {
  55. if em.Client == nil {
  56. if e := em.NewClient(); e != nil {
  57. err = e
  58. return
  59. }
  60. }
  61. singleSendMailRequest := &dm.SingleSendMailRequest{}
  62. singleSendMailRequest.SetAccountName(AliyunEmailAccountName)
  63. singleSendMailRequest.SetAddressType(1)
  64. singleSendMailRequest.SetReplyToAddress(false)
  65. singleSendMailRequest.SetSubject(item.Subject)
  66. singleSendMailRequest.SetToAddress(item.Email)
  67. singleSendMailRequest.SetHtmlBody(item.HtmlBody)
  68. singleSendMailRequest.SetFromAlias(item.FromAlias)
  69. runtime := &util.RuntimeOptions{}
  70. tryErr := func() error {
  71. res, e := em.Client.SingleSendMailWithOptions(singleSendMailRequest, runtime)
  72. if e != nil {
  73. return e
  74. }
  75. // 请求成功
  76. if tea.Int32Value(res.StatusCode) == 200 {
  77. ok = true
  78. }
  79. resByte, e := json.Marshal(res.Body)
  80. if e != nil {
  81. return e
  82. }
  83. result = string(resByte)
  84. return nil
  85. }()
  86. if tryErr != nil {
  87. var e = &tea.SDKError{}
  88. if t, ok := tryErr.(*tea.SDKError); ok {
  89. e = t
  90. } else {
  91. e.Message = tea.String(tryErr.Error())
  92. }
  93. err = e
  94. errByte, _ := json.Marshal(err)
  95. result = string(errByte)
  96. }
  97. return
  98. }
  99. // BatchSendEmail 批量推送邮件
  100. func (em *AliyunEmail) BatchSendEmail(list []*EnglishReportSendEmailRequest) (results []*EnglishReportSendEmailResult, err error) {
  101. results = make([]*EnglishReportSendEmailResult, 0)
  102. if len(list) == 0 {
  103. return
  104. }
  105. if e := em.NewClient(); e != nil {
  106. err = e
  107. return
  108. }
  109. // sendSingleMail接口有QPS100的限制, 保险起见每秒只请求50次接口
  110. max := 50
  111. c := 0
  112. for i := range list {
  113. if c >= max {
  114. time.Sleep(time.Second)
  115. c = 0
  116. }
  117. c += 1
  118. dataByte, _ := json.Marshal(list[i])
  119. ok, result, _ := em.SendEmail(list[i])
  120. results = append(results, &EnglishReportSendEmailResult{
  121. ReportId: list[i].ReportId,
  122. EmailId: list[i].EmailId,
  123. Email: list[i].Email,
  124. Ok: ok,
  125. SendData: string(dataByte),
  126. ResultData: result,
  127. Source: 1,
  128. })
  129. }
  130. return
  131. }