mail.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/macross/conf"
  6. "go-common/app/admin/main/macross/model/mail"
  7. "go-common/app/admin/main/macross/tools"
  8. "go-common/library/log"
  9. "io"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. gomail "gopkg.in/gomail.v2"
  14. )
  15. // SendMail send mail
  16. func (s *Service) SendMail(c context.Context, m *mail.Mail, attach *mail.Attach) (err error) {
  17. var (
  18. toUsers []string
  19. ccUsers []string
  20. bccUsers []string
  21. msg = gomail.NewMessage()
  22. )
  23. msg.SetAddressHeader("From", conf.Conf.Property.Mail.Address, conf.Conf.Property.Mail.Name) // 发件人
  24. for _, ads := range m.ToAddresses {
  25. toUsers = append(toUsers, msg.FormatAddress(ads.Address, ads.Name))
  26. }
  27. for _, ads := range m.CcAddresses {
  28. ccUsers = append(ccUsers, msg.FormatAddress(ads.Address, ads.Name))
  29. }
  30. for _, ads := range m.BccAddresses {
  31. bccUsers = append(bccUsers, msg.FormatAddress(ads.Address, ads.Name))
  32. }
  33. msg.SetHeader("To", toUsers...)
  34. msg.SetHeader("Subject", m.Subject) // 主题
  35. if len(ccUsers) > 0 {
  36. msg.SetHeader("Cc", ccUsers...)
  37. }
  38. if len(bccUsers) > 0 {
  39. msg.SetHeader("Bcc", bccUsers...)
  40. }
  41. if m.Type == mail.TypeTextHTML {
  42. msg.SetBody("text/html", m.Body)
  43. } else {
  44. msg.SetBody("text/plain", m.Body)
  45. }
  46. // 附件处理
  47. if attach != nil {
  48. tmpSavePath := filepath.Join(os.TempDir(), "mail_tmp")
  49. err = os.MkdirAll(tmpSavePath, 0755)
  50. if err != nil {
  51. log.Error("os.MkdirAll error(%v)", err)
  52. return
  53. }
  54. destFilePath := filepath.Join(tmpSavePath, attach.Name)
  55. destFile, cErr := os.Create(destFilePath)
  56. if cErr != nil {
  57. log.Error("os.Create(%s) error(%v)", destFilePath, cErr)
  58. return cErr
  59. }
  60. defer os.RemoveAll(tmpSavePath)
  61. io.Copy(destFile, attach.File)
  62. // 如果 zip 文件需要解压以后放在邮件附件中
  63. if attach.ShouldUnzip && strings.HasSuffix(attach.Name, ".zip") {
  64. unzipFilePath := filepath.Join(tmpSavePath, "unzip")
  65. err = os.MkdirAll(tmpSavePath, 0755)
  66. if err != nil {
  67. log.Error("os.MkdirAll error(%v)", err)
  68. return
  69. }
  70. err = tools.Unzip(destFilePath, unzipFilePath)
  71. if err != nil {
  72. log.Error("unzip(%s, %s) error(%v)", destFilePath, unzipFilePath, err)
  73. return
  74. }
  75. err = filepath.Walk(unzipFilePath, func(path string, f os.FileInfo, err error) error {
  76. if err != nil {
  77. log.Error("filepath.Walk error(%v)", err)
  78. return err
  79. }
  80. if f == nil {
  81. errMsg := "found no file"
  82. err = fmt.Errorf(errMsg)
  83. log.Error(errMsg)
  84. return err
  85. }
  86. if f.IsDir() {
  87. return nil
  88. }
  89. msg.Attach(path)
  90. return err
  91. })
  92. } else {
  93. msg.Attach(destFilePath)
  94. }
  95. }
  96. d := gomail.NewDialer(
  97. conf.Conf.Property.Mail.Host,
  98. conf.Conf.Property.Mail.Port,
  99. conf.Conf.Property.Mail.Address,
  100. conf.Conf.Property.Mail.Pwd,
  101. )
  102. if err = d.DialAndSend(msg); err != nil {
  103. log.Error("Send mail Fail(%v) diff(%s)", msg, err)
  104. return
  105. }
  106. return
  107. }