aliyun_email.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  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. "hongze/hongze_yb_en_api/global"
  10. "hongze/hongze_yb_en_api/utils"
  11. "time"
  12. )
  13. var (
  14. AliyunEmailAccountName = "ficcemail@hzmail.hzinsights.com"
  15. AliyunEmailAccessKeyId = "LTAIFMZYQhS2BTvW"
  16. AliyunEmailAccessKeySecret = "12kk1ptCHoGWedhBnKRVW5hRJzq9Fq"
  17. AliyunEmailReplyAddress = "ficcemail@hzinsights.com"
  18. AliyunEmailReplyAddressAlias = "弘则研究"
  19. )
  20. // AliyunEmail 阿里云邮件
  21. type AliyunEmail struct {
  22. Client *dm.Client
  23. }
  24. // NewClient
  25. func (em *AliyunEmail) NewClient() (err error) {
  26. config := &openapi.Config{
  27. AccessKeyId: tea.String(AliyunEmailAccessKeyId),
  28. AccessKeySecret: tea.String(AliyunEmailAccessKeySecret),
  29. }
  30. // 访问的域名
  31. config.Endpoint = tea.String("dm.aliyuncs.com")
  32. _result, _err := dm.NewClient(config)
  33. em.Client = _result
  34. err = _err
  35. return
  36. }
  37. // AliyunEmailResult 邮件推送响应体
  38. type AliyunEmailResult struct {
  39. Code string `description:"错误码"`
  40. StatusCode int `description:"状态码"`
  41. Message string `description:"响应信息"`
  42. Data string `description:"请求体"`
  43. Description string `description:"描述"`
  44. EnvId string `description:"请求成功-事件ID"`
  45. RequestId string `description:"请求成功-请求ID"`
  46. }
  47. // AliyunEmailResultData 邮件推送响应内容
  48. type AliyunEmailResultData struct {
  49. Code string `description:"错误码"`
  50. HostId string `description:""`
  51. Message string `description:"响应信息"`
  52. Recommend string `description:""`
  53. RequestId string `description:"请求ID"`
  54. StatusCode int `description:"状态码"`
  55. }
  56. //
  57. //// SendEmail 邮件推送
  58. //func (em *AliyunEmail) SendEmail(item *english_report.EnglishReportSendEmailRequest) (ok bool, result string, err error) {
  59. // if em.Client == nil {
  60. // if e := em.NewClient(); e != nil {
  61. // err = e
  62. // return
  63. // }
  64. // }
  65. // singleSendMailRequest := &dm.SingleSendMailRequest{}
  66. // singleSendMailRequest.SetAccountName(AliyunEmailAccountName)
  67. // singleSendMailRequest.SetAddressType(1)
  68. // singleSendMailRequest.SetReplyToAddress(false)
  69. // singleSendMailRequest.SetSubject(item.Subject)
  70. // singleSendMailRequest.SetToAddress(item.Email)
  71. // singleSendMailRequest.SetHtmlBody(item.HtmlBody)
  72. // singleSendMailRequest.SetFromAlias(item.FromAlias)
  73. //
  74. // runtime := &util.RuntimeOptions{}
  75. // tryErr := func() error {
  76. // res, e := em.Client.SingleSendMailWithOptions(singleSendMailRequest, runtime)
  77. // if e != nil {
  78. // return e
  79. // }
  80. // // 请求成功
  81. // if tea.Int32Value(res.StatusCode) == 200 {
  82. // ok = true
  83. // }
  84. // resByte, e := json.Marshal(res.Body)
  85. // if e != nil {
  86. // return e
  87. // }
  88. // result = string(resByte)
  89. // return nil
  90. // }()
  91. //
  92. // if tryErr != nil {
  93. // var e = &tea.SDKError{}
  94. // if t, ok := tryErr.(*tea.SDKError); ok {
  95. // e = t
  96. // } else {
  97. // e.Message = tea.String(tryErr.Error())
  98. // }
  99. // err = e
  100. // errByte, _ := json.Marshal(err)
  101. // result = string(errByte)
  102. // }
  103. // return
  104. //}
  105. //
  106. //// BatchSendEmail 批量推送邮件
  107. //func (em *AliyunEmail) BatchSendEmail(list []*english_report.EnglishReportSendEmailRequest) (results []*english_report.EnglishReportSendEmailResult, err error) {
  108. // results = make([]*english_report.EnglishReportSendEmailResult, 0)
  109. // if len(list) == 0 {
  110. // return
  111. // }
  112. // if e := em.NewClient(); e != nil {
  113. // err = e
  114. // return
  115. // }
  116. //
  117. // // sendSingleMail接口有QPS100的限制, 保险起见每秒只请求50次接口
  118. // max := 50
  119. // c := 0
  120. // for i := range list {
  121. // if c >= max {
  122. // time.Sleep(time.Second)
  123. // c = 0
  124. // }
  125. // c += 1
  126. //
  127. // dataByte, _ := json.Marshal(list[i])
  128. // ok, result, _ := em.SendEmail(list[i])
  129. // results = append(results, &english_report.EnglishReportSendEmailResult{
  130. // ReportId: list[i].ReportId,
  131. // EmailId: list[i].EmailId,
  132. // Email: list[i].Email,
  133. // Ok: ok,
  134. // SendData: string(dataByte),
  135. // ResultData: result,
  136. // Source: 1,
  137. // })
  138. // }
  139. // return
  140. //}
  141. // sendEmailVerificationCode 邮件验证码推送
  142. func (em *AliyunEmail) sendEmailVerificationCode(email, content string) (ok bool, result string, err error) {
  143. if em.Client == nil {
  144. if e := em.NewClient(); e != nil {
  145. err = e
  146. return
  147. }
  148. }
  149. singleSendMailRequest := &dm.SingleSendMailRequest{}
  150. singleSendMailRequest.SetAccountName(AliyunEmailAccountName)
  151. singleSendMailRequest.SetAddressType(1)
  152. singleSendMailRequest.SetReplyToAddress(false)
  153. singleSendMailRequest.SetSubject("Your Email Verification Code")
  154. singleSendMailRequest.SetToAddress(email)
  155. singleSendMailRequest.SetHtmlBody(content)
  156. singleSendMailRequest.SetFromAlias("Horizon FICC")
  157. runtime := &util.RuntimeOptions{}
  158. tryErr := func() error {
  159. res, e := em.Client.SingleSendMailWithOptions(singleSendMailRequest, runtime)
  160. if e != nil {
  161. return e
  162. }
  163. // 请求成功
  164. if tea.Int32Value(res.StatusCode) == 200 {
  165. ok = true
  166. }
  167. resByte, e := json.Marshal(res.Body)
  168. if e != nil {
  169. return e
  170. }
  171. result = string(resByte)
  172. return nil
  173. }()
  174. if tryErr != nil {
  175. var e = &tea.SDKError{}
  176. if t, ok := tryErr.(*tea.SDKError); ok {
  177. e = t
  178. } else {
  179. e.Message = tea.String(tryErr.Error())
  180. }
  181. err = e
  182. errByte, _ := json.Marshal(err)
  183. result = string(errByte)
  184. }
  185. return
  186. }
  187. // SendEmailVerificationCode 邮件验证码
  188. func (em *AliyunEmail) SendEmailCode(name, email string) (results []*EnglishReportSendEmailCodeResult, err error) {
  189. results = make([]*EnglishReportSendEmailCodeResult, 0)
  190. if e := em.NewClient(); e != nil {
  191. err = e
  192. return
  193. }
  194. msgCode := utils.GetRandDigit(4)
  195. content := "Hi " + name + ":</br>Please enter this verification code on the registration page so we can make sure it's you:</br><p>" + msgCode + "</p>This code will expire in 15 minutes.</br>" +
  196. "If you didn't initiate this request, or need help with your registration, please let us know at: stephanie@hzinsights.com </br>Horizon Research Team"
  197. dataByte, _ := json.Marshal(content)
  198. ok, result, _ := em.sendEmailVerificationCode(email, content)
  199. results = append(results, &EnglishReportSendEmailCodeResult{
  200. Email: email,
  201. Ok: ok,
  202. SendData: string(dataByte),
  203. ResultData: result,
  204. Source: 1,
  205. })
  206. _ = global.Redis.SetEX(context.TODO(), email, msgCode, 15*time.Minute)
  207. return
  208. }
  209. // EnglishReportSendEmailCodeResult 英文研报验证码-推送邮件响应体
  210. type EnglishReportSendEmailCodeResult struct {
  211. Email string `description:"邮箱地址"`
  212. Ok bool `description:"是否推送成功"`
  213. SendData string `description:"请求数据-JSON"`
  214. ResultData string `description:"推送结果-JSON"`
  215. Source int `description:"服务来源:1-阿里云;2-腾讯云"`
  216. }