email.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package dao
  2. import (
  3. "os"
  4. "go-common/app/job/main/mcn/conf"
  5. "go-common/library/log"
  6. "gopkg.in/gomail.v2"
  7. )
  8. // SendMail send the email.
  9. func (d *Dao) SendMail(body string, subject string, send []string) (err error) {
  10. log.Info("send mail send:%v", send)
  11. msg := gomail.NewMessage()
  12. msg.SetHeader("From", conf.Conf.MailConf.Username)
  13. msg.SetHeader("To", send...)
  14. msg.SetHeader("Subject", subject)
  15. msg.SetBody("text/html", body, gomail.SetPartEncoding(gomail.Base64))
  16. if err = d.email.DialAndSend(msg); err != nil {
  17. log.Error("s.email.DialAndSend error(%v)", err)
  18. return
  19. }
  20. return
  21. }
  22. // SendMailAttach send the email.
  23. func (d *Dao) SendMailAttach(filename string, subject string, send []string) (err error) {
  24. msg := gomail.NewMessage()
  25. msg.SetHeader("From", conf.Conf.MailConf.Username)
  26. msg.SetHeader("To", send...)
  27. msg.SetHeader("Subject", subject)
  28. msg.Attach(filename)
  29. if err = d.email.DialAndSend(msg); err != nil {
  30. log.Error("s.email.DialAndSend error(%v)", err)
  31. return
  32. }
  33. err = os.Remove(filename)
  34. return
  35. }