aliyun_email.go 3.5 KB

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