email.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package services
  2. import (
  3. "crypto/tls"
  4. "eta/eta_api/models"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "gopkg.in/gomail.v2"
  8. "strconv"
  9. "strings"
  10. )
  11. func SendEmailToCompany() {
  12. // 获取收件人列表
  13. emailCond := " AND enabled = 1 AND status in (1,2) "
  14. //emailCond := ""
  15. emailPars := make([]interface{}, 0)
  16. emails, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  17. if e != nil {
  18. fmt.Println("获取收件人列表失败, Err: " + e.Error())
  19. return
  20. }
  21. if len(emails) == 0 {
  22. fmt.Println("收件人列表为空")
  23. return
  24. }
  25. // TODO:这是HTML模板内容
  26. template := `<!DOCTYPE html>
  27. <html lang="en">
  28. <head>
  29. <meta charset="UTF-8" />
  30. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  31. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  32. <title>Document</title>
  33. </head>
  34. <body>
  35. <div>
  36. <p>Hello,</p>
  37. <p></p>
  38. <p> We are pleased to invite you to the launch of our <span style="font-weight: 700;">Mon. Date with HI</span> , a weekly event where we share timely updates and expert analysis on Global Macro, Base Metal, Ferrous Metal, and Energy markets.</p>
  39. <h3 style="font-weight: 700;">Event Details:</h3>
  40. <ul>
  41. <li>Date: August 26th</li>
  42. <li>Time: 5:00 PM (Shanghai/Singapore) | 10:00 AM (London)</li>
  43. <li>Platform: Zoom (the same link will be used for all future sessions)</li>
  44. </ul>
  45. <h3 style="font-weight: 700;">Join Zoom Meeting:</h3>
  46. <a href="https://us06web.zoom.us/j/84723517075?pwd=3GHNOCHq6sAgBGXsA7qtX9r1PuA1uK.1" target="_blank">https://us06web.zoom.us/j/84723517075?pwd=3GHNOCHq6sAgBGXsA7qtX9r1PuA1uK.1</a>
  47. <p>
  48. <span style="font-weight: 700;">Meeting ID:</span>
  49. <span>847 2351 7075</span>
  50. </p>
  51. <p>
  52. <span style="font-weight: 700;">Passcode:</span>
  53. <span>564439</span>
  54. </p>
  55. <p style="font-weight: 700;">Theme for Today’s Session: Diverging Expectations between Market and Policymakers</p>
  56. <p>Our analysts will discuss the impact of the current macro environment on commodity markets, exploring both short-term challenges and longer-term outlooks.<br>This series will be a recurring event every Monday at the same time. </p>
  57. <h3 style="font-weight: 700;">Please note:</h3>
  58. <ul>
  59. <li>This event will be conducted in listen-only mode. However, participants are encouraged to ask questions via the chat function during the Q&A segments.</li>
  60. <li>For privacy concerns, you may change your display name upon joining the event.</li>
  61. </ul>
  62. <p></p>
  63. <p>We look forward to your participation.</p>
  64. <p></p>
  65. <p>Best regards,<br>Horizon Insights</p>
  66. <img style="max-width: 375px;width: 100%;" src="https://hzstatic.hzinsights.com/static/images/202408/20240826/z0qJ4cpM5mIaHrHv0BaMsNXYkLKa.jpg" alt="">
  67. </div>
  68. </body>
  69. </html>`
  70. // 推送信息
  71. sendData := make([]*EnglishReportSendEmailRequest, 0)
  72. for i := range emails {
  73. r := new(EnglishReportSendEmailRequest)
  74. r.EmailId = emails[i].Id
  75. r.Email = strings.Replace(emails[i].Email, " ", "", -1)
  76. r.Subject = "Invitation to Weekly Call with Horizon Insights Analysts: Mon. Date with HI" // TODO:这是主题
  77. r.FromAlias = "Horizon FICC" // TODO:这是推送人(中文)
  78. r.HtmlBody = template
  79. sendData = append(sendData, r)
  80. }
  81. if len(sendData) == 0 {
  82. fmt.Println("无邮件可推送")
  83. return
  84. }
  85. // 请求阿里云接口批量推送
  86. aliEmail := new(AliyunEmail)
  87. resultList, e := aliEmail.BatchSendEmail(sendData)
  88. if e != nil {
  89. fmt.Println("批量推送失败, Err: " + e.Error())
  90. return
  91. }
  92. for _, r := range resultList {
  93. utils.FileLog.Info("email: %s, ok: %v, res: %s", r.Email, r.Ok, r.ResultData)
  94. if r.Ok {
  95. fmt.Println("发送成功")
  96. } else {
  97. fmt.Println("发送失败:" + r.ResultData)
  98. }
  99. }
  100. }
  101. type SendEmailReq struct {
  102. Title string `description:"标题"`
  103. Content string `description:"内容"`
  104. ToUser []string `description:"收信人邮箱"`
  105. }
  106. func SendEmail(req SendEmailReq) (success bool, err error) {
  107. if req.Title == "" {
  108. err = fmt.Errorf("邮件主题不可为空")
  109. return
  110. }
  111. if req.Content == "" {
  112. err = fmt.Errorf("邮件内容不可为空")
  113. return
  114. }
  115. if len(req.ToUser) <= 0 {
  116. err = fmt.Errorf("收信人不可为空")
  117. return
  118. }
  119. // 邮箱配置
  120. confMap, e := models.GetBusinessConf()
  121. if e != nil {
  122. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  123. return
  124. }
  125. checkArr := []string{
  126. models.BusinessConfEmailServerHost, models.BusinessConfEmailServerPort,
  127. models.BusinessConfEmailSender, models.BusinessConfEmailSenderUserName,
  128. models.BusinessConfEmailSenderPassword,
  129. }
  130. for _, v := range checkArr {
  131. if confMap[v] == "" {
  132. err = fmt.Errorf("%s配置有误", v)
  133. return
  134. }
  135. }
  136. port, _ := strconv.Atoi(confMap[models.BusinessConfEmailServerPort])
  137. if port <= 0 {
  138. port = 587 // 默认587端口
  139. }
  140. m := gomail.NewMessage()
  141. m.SetHeader("From", confMap[models.BusinessConfEmailSender])
  142. m.SetHeader("To", req.ToUser...)
  143. m.SetHeader("Subject", req.Title)
  144. m.SetBody("text/html", req.Content)
  145. d := gomail.NewDialer(confMap[models.BusinessConfEmailServerHost], port, confMap[models.BusinessConfEmailSenderUserName], confMap[models.BusinessConfEmailSenderPassword])
  146. // 解决x509报错的问题。证书不通过。跳过证书验证
  147. config := &tls.Config{ServerName: confMap[models.BusinessConfEmailServerHost], InsecureSkipVerify: true}
  148. d.TLSConfig = config
  149. if e = d.DialAndSend(m); e != nil {
  150. err = fmt.Errorf("邮件发送失败, Err: %s", e.Error())
  151. return
  152. }
  153. success = true
  154. return
  155. }