sendMail.go 7.5 KB

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