smtp.go 1006 B

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