mail.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package mail
  2. import (
  3. "bytes"
  4. "fmt"
  5. "text/template"
  6. "go-common/app/tool/saga/conf"
  7. "go-common/app/tool/saga/model"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. gomail "gopkg.in/gomail.v2"
  11. )
  12. // SendMail2 ...
  13. func SendMail2(addr *model.MailAddress, subject string, data string) (err error) {
  14. var (
  15. msg = gomail.NewMessage()
  16. )
  17. msg.SetAddressHeader("From", conf.Conf.Property.Mail.Address, conf.Conf.Property.Mail.Name)
  18. msg.SetHeader("To", msg.FormatAddress(addr.Address, addr.Name))
  19. msg.SetHeader("Subject", subject)
  20. msg.SetBody("text/plain", data)
  21. d := gomail.NewDialer(
  22. conf.Conf.Property.Mail.Host,
  23. conf.Conf.Property.Mail.Port,
  24. conf.Conf.Property.Mail.Address,
  25. conf.Conf.Property.Mail.Pwd,
  26. )
  27. if err = d.DialAndSend(msg); err != nil {
  28. err = errors.WithMessage(err, fmt.Sprintf("Send mail (%+v,%s,%s) failed", addr, subject, data))
  29. return
  30. }
  31. log.Info("Send mail (%+v,%s,%s) success", addr, subject, data)
  32. return
  33. }
  34. // SendMail send mail
  35. func SendMail(m *model.Mail, data *model.MailData) (err error) {
  36. var (
  37. toUsers []string
  38. msg = gomail.NewMessage()
  39. buf = &bytes.Buffer{}
  40. )
  41. msg.SetAddressHeader("From", conf.Conf.Property.Mail.Address, conf.Conf.Property.Mail.Name) // 发件人
  42. for _, ads := range m.ToAddress {
  43. toUsers = append(toUsers, msg.FormatAddress(ads.Address, ads.Name))
  44. }
  45. t := template.New("MR Mail")
  46. if t, err = t.Parse(mailTPL); err != nil {
  47. log.Error("tpl.Parse(%s) error(%+v)", mailTPL, errors.WithStack(err))
  48. return
  49. }
  50. err = t.Execute(buf, data)
  51. if err != nil {
  52. log.Error("t.Execute error(%+v)", errors.WithStack(err))
  53. return
  54. }
  55. msg.SetHeader("To", toUsers...)
  56. msg.SetHeader("Subject", m.Subject) // 主题
  57. msg.SetBody("text/html", buf.String()) // 正文
  58. d := gomail.NewDialer(
  59. conf.Conf.Property.Mail.Host,
  60. conf.Conf.Property.Mail.Port,
  61. conf.Conf.Property.Mail.Address,
  62. conf.Conf.Property.Mail.Pwd,
  63. )
  64. if err = d.DialAndSend(msg); err != nil {
  65. log.Error("Send mail Fail(%v) diff(%s)", msg, err)
  66. return
  67. }
  68. return
  69. }
  70. // SendMail3 SendMail all parameter
  71. func SendMail3(from string, sender string, senderPwd string, m *model.Mail, data *model.MailData) (err error) {
  72. var (
  73. toUsers []string
  74. msg = gomail.NewMessage()
  75. buf = &bytes.Buffer{}
  76. )
  77. msg.SetAddressHeader("From", from, sender) // 发件人
  78. for _, ads := range m.ToAddress {
  79. toUsers = append(toUsers, msg.FormatAddress(ads.Address, ads.Name))
  80. }
  81. t := template.New("MR Mail")
  82. if t, err = t.Parse(mailTPL3); err != nil {
  83. log.Error("tpl.Parse(%s) error(%+v)", mailTPL3, errors.WithStack(err))
  84. return
  85. }
  86. err = t.Execute(buf, data)
  87. if err != nil {
  88. log.Error("t.Execute error(%+v)", errors.WithStack(err))
  89. return
  90. }
  91. msg.SetHeader("To", toUsers...)
  92. msg.SetHeader("Subject", m.Subject) // 主题
  93. msg.SetBody("text/html", buf.String()) // 正文
  94. d := gomail.NewDialer(
  95. "smtp.exmail.qq.com",
  96. 465,
  97. from,
  98. senderPwd,
  99. )
  100. if err = d.DialAndSend(msg); err != nil {
  101. log.Error("Send mail Fail(%v) diff(%s)", msg, err)
  102. return
  103. }
  104. return
  105. }