smtp.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package utl
  2. import (
  3. "gopkg.in/gomail.v2"
  4. )
  5. type IMessager interface {
  6. Send([]string, string, string) error
  7. }
  8. type smtp struct {
  9. host string
  10. port int
  11. username, password string
  12. alias string
  13. }
  14. func NewSmtp(host string, port int, username, password, alias string) IMessager {
  15. return &smtp{
  16. host: host,
  17. port: port,
  18. username: username,
  19. password: password,
  20. alias: alias,
  21. }
  22. }
  23. func (self *smtp) Send(to []string, subject, body string) error {
  24. m := gomail.NewMessage( /* gomail.SetEncoding(gomail.Base64) */ )
  25. m.SetHeader("From", m.FormatAddress(self.username, self.alias))
  26. m.SetHeader("To", to...)
  27. m.SetHeader("Subject", subject)
  28. m.SetBody("text/html", body)
  29. //m.Attach("/tmp/foo.txt",
  30. // gomail.Rename("foo.txt"),
  31. // gomail.SetHeader(map[string][]string{
  32. // "Content-Disposition": []string{
  33. // fmt.Sprintf(`attachment; filename="%s"`, mime.QEncoding.Encode("UTF-8", name)),
  34. // },
  35. // }),
  36. //)
  37. d := gomail.NewDialer(self.host, self.port, self.username, self.password)
  38. return d.DialAndSend(m)
  39. }
  40. type disableMessager struct{}
  41. func NewDisableMessager() IMessager {
  42. return &disableMessager{}
  43. }
  44. func (self *disableMessager) Send(to []string, subject, body string) error {
  45. return nil
  46. }