sendMail.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/mail"
  4. "context"
  5. "fmt"
  6. "github.com/gogf/gf/v2/errors/gerror"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "strings"
  9. "time"
  10. )
  11. type (
  12. mailConfig struct {
  13. Addr string
  14. Port int
  15. Pwd string
  16. User string
  17. }
  18. invoiceRecord struct {
  19. InvoiceType string `json:"invoice_type"`
  20. Mail string `json:"mail"`
  21. TaxpayerIdentnum string `json:"taxpayer_identnum"`
  22. CompanyName string `json:"company_name"`
  23. Phone string `json:"phone"`
  24. SourceUrl string `json:"source_url"`
  25. OrderCode string `json:"order_code"`
  26. InvoiceVariety string `json:"invoice_variety"`
  27. InvoiceContent string `json:"invoice_content"`
  28. InvoiceOrderCode string `json:"invoice_order_code"`
  29. }
  30. )
  31. var (
  32. GmailAuth []*mail.GmailAuth
  33. mailConfigArr []*mailConfig
  34. )
  35. const (
  36. emailHtml_header = `<!DOCTYPE html>
  37. <html lang="en">
  38. <head>
  39. <meta charset="UTF-8">
  40. <title>电子发票</title>
  41. </head>
  42. <style type="text/css">
  43. html, body,a,div,p {
  44. margin:0;
  45. padding:0;
  46. }
  47. .jymail a{text-decoration: none !important;font-family: "PingFang SC";font-size: 15px;}
  48. .email{width: 680px;border: 1px solid #E0E0E0;background: #fff;margin: 20px auto;border-radius: 8px;}
  49. .jymail .top{height: 80px;width:100%%;padding: 0 40px;margin:0 auto;background-color: #2CB7CA;border-radius:8px 8px 0 0;clear: both;box-sizing: border-box;}
  50. .jymail .left{float:left;width:120px;height: 32px;margin: 20px 0;}
  51. .jymail .left img{width:100%%;height: 100%%;}
  52. .jymail .right{float: right;margin: 30px 0;}
  53. .jymail .right a{color: #fff;font-size: 14px;line-height: 18px;}
  54. .jymail .middle{width: 100%%;padding: 64px 40px;box-sizing: border-box;}
  55. .jymail .user{font-size: 18px;line-height: 24px;color: #1D1D1D;padding-bottom: 32px;}
  56. .jymail .text{font-size: 13px;line-height: 20px;color: #1D1D1D;}
  57. .jymail .download{display: inline-block;width: 90px;height: 30px;background-color: #2CB7CA;color: #fff;text-align: center;border-radius: 6px;line-height: 30px;}
  58. .jymail .warning{font-size: 12px;line-height: 20px;color: red;color: #FE737A;}
  59. .jymail .bottom{margin-top:32px;border-top: 4px solid #2CB7CA;background: #F5F5FB;padding: 32px 40px;box-sizing: border-box;border-bottom-left-radius: 8px;border-bottom-right-radius: 8px;}
  60. .jymail .item{margin-bottom: 10px;font-size: 12px;line-height: 24px;}
  61. .jymail .label{display: inline-block; min-width: 92px;color: #686868;}
  62. .jymail .value{color: #1D1D1D;}
  63. </style>
  64. <body>
  65. <div class="email jymail">
  66. <div class="top">
  67. <div class="left">
  68. <img src="https://www.jianyu360.com/images/pc/logo.png">
  69. </div>
  70. <div class="right">
  71. <a href="https://www.jianyu360.com">剑鱼首页</a>
  72. <span style="color: #fff;margin: 0 12px;">|</span>
  73. <a href="https://www.jianyu360.com/front/swordfish/toMyOrder">我的订单</a>
  74. </div>
  75. </div>
  76. <div class="middle">
  77. <p class="user">尊敬的用户您好!</p><b></b>
  78. <p class="text">感谢您在剑鱼标讯(<a href="https://www.jianyu360.com" style="color: #2CB7CA;">jianyu360.com</a>)购物!</p>
  79. <p class="text">剑鱼标讯已为您的订单: <span id="order_code">%s</span> 开具%s,总共 1 张。</p>
  80. <div style="padding: 32px 0;">
  81. %s
  82. </div>
  83. <p class="warning">安全提醒:剑鱼标讯平台及销售商不会以订单异常、系统升级等理由,通过任何方式发送给您退款链接。请您谨防钓鱼链接或诈骗电话!</p>
  84. <div class="bottom" >
  85. <p style="padding-bottom:16px;color: #2CB7CA; font-size: 14px;line-height: 24px;">发票信息</p>
  86. <p class="item">
  87. <span class="label">发票类型:</span>
  88. <span class="value">%s</span>
  89. </p>
  90. <p class="item">
  91. <span class="label">发票内容:</span>
  92. <span class="value">%s</span>
  93. </p>`
  94. emailHtml_gs = `<p class="item">
  95. <span class="label">公司名称:</span>
  96. <span class="value">%s</span>
  97. </p>
  98. <p class="item">
  99. <span class="label">纳税人识别号:</span>
  100. <span class="value">%s</span>
  101. </p>
  102. <p class="item">
  103. <span class="label">联系电话:</span>
  104. <span class="value">%s</span>
  105. </p>
  106. <p class="item">
  107. <span class="label">电子邮箱:</span>
  108. <span class="value">%s</span>
  109. </p>
  110. </div>
  111. </div>
  112. </div>
  113. </body>
  114. </html>`
  115. emailHtml_gr = `<p class="item">
  116. <span class="label">联系电话:</span>
  117. <span class="value">%s</span>
  118. </p>
  119. <p class="item">
  120. <span class="label">电子邮箱:</span>
  121. <span class="value">%s</span>
  122. </p>
  123. </div>
  124. </div>
  125. </div>
  126. </body>
  127. </html>`
  128. )
  129. func init() {
  130. ctx := context.Background()
  131. err := g.Cfg().MustGet(ctx, "mailConfig").Struct(&mailConfigArr)
  132. if err != nil {
  133. g.Log().Panic(ctx, "初始化邮件配置异常")
  134. }
  135. for _, v := range mailConfigArr {
  136. GmailAuth = append(GmailAuth, &mail.GmailAuth{
  137. SmtpHost: v.Addr,
  138. SmtpPort: v.Port,
  139. User: v.User,
  140. Pwd: v.Pwd,
  141. })
  142. }
  143. //SendMail(ctx, "wangkaiyue@topnet.net.cn", "<h1>Hello world</h1>", "测试邮件")
  144. }
  145. func SendMail(ctx context.Context, targetMail, html, title string) error {
  146. for k, v := range GmailAuth {
  147. if mail.GSendMail("剑鱼标讯", targetMail, "", "", title, html, "", "", v) {
  148. g.Log().Infof(ctx, "发送邮件成功%s", targetMail)
  149. break
  150. }
  151. if k < len(GmailAuth)-1 {
  152. g.Log().Infof(ctx, "发送邮件失败%s!3s后使用其他邮箱尝试", targetMail)
  153. } else {
  154. return gerror.Newf("发送邮件失败%s![final]", targetMail)
  155. }
  156. time.Sleep(time.Second * 3)
  157. }
  158. return nil
  159. }
  160. func SendInvoiceSuccessMail(ctx context.Context, callBackId string) error {
  161. var (
  162. queryItem, queryValue string
  163. err error
  164. record invoiceRecord
  165. )
  166. queryItem, queryValue, err = GetQueryObj(callBackId)
  167. if err != nil {
  168. return err
  169. }
  170. res, err := g.DB().GetOne(ctx, fmt.Sprintf("SELECT invoice_type,mail,taxpayer_identnum,company_name,phone,source_url,order_code,invoice_variety,invoice_content,invoice_order_code FROM invoice WHERE %s = ? AND invoice_status=1 order by id desc limit 1", queryItem), queryValue)
  171. if err != nil {
  172. return gerror.Wrapf(err, "未查询到订单发票信息 %s:%s", queryItem, queryValue)
  173. }
  174. if err = res.Struct(&record); err != nil {
  175. return gerror.Wrapf(err, "格式化发票信息异常")
  176. }
  177. if record.Mail == "" {
  178. return nil
  179. }
  180. return SendMail(ctx, record.Mail, record.GetMailHtmlContext(), "电子发票")
  181. }
  182. func (ir *invoiceRecord) GetMailHtmlContext() string {
  183. var (
  184. tType = "电子普通发票"
  185. invoiceContext = "信息技术服务-技术服务费"
  186. showOrderCode = ir.OrderCode
  187. )
  188. if strings.Contains(ir.InvoiceVariety, "专用") {
  189. tType = "电子专用发票"
  190. }
  191. if ir.InvoiceContent != "" {
  192. invoiceContext = ir.InvoiceContent
  193. }
  194. if ir.InvoiceOrderCode != "" {
  195. showOrderCode = ir.InvoiceOrderCode
  196. }
  197. switch ir.InvoiceType {
  198. case "单位":
  199. return fmt.Sprintf(emailHtml_header+emailHtml_gs, showOrderCode, tType, fmt.Sprintf("<a href=\"%s\" download class=\"download\">查看发票</a>", ir.SourceUrl), invoiceContext, tType, ir.CompanyName, ir.TaxpayerIdentnum, ir.Phone, ir.Mail)
  200. case "个人":
  201. return fmt.Sprintf(emailHtml_header+emailHtml_gr, showOrderCode, tType, fmt.Sprintf("<a href=\"%s\" download class=\"download\">查看发票</a>", ir.SourceUrl), invoiceContext, tType, ir.Phone, ir.Mail)
  202. }
  203. return ""
  204. }
  205. func GetQueryObj(callBackId string) (query, value string, err error) {
  206. arr := strings.Split(callBackId, ":")
  207. if len(arr) != 2 {
  208. err = gerror.Newf("callBackId id异常%s", callBackId)
  209. return
  210. }
  211. query, value = arr[0], arr[1]
  212. return
  213. }