| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package utl
- import (
- "gopkg.in/gomail.v2"
- )
- type IMessager interface {
- Send([]string, string, string) error
- }
- type smtp struct {
- host string
- port int
- username, password string
- alias string
- }
- func NewSmtp(host string, port int, username, password, alias string) IMessager {
- return &smtp{
- host: host,
- port: port,
- username: username,
- password: password,
- alias: alias,
- }
- }
- func (self *smtp) Send(to []string, subject, body string) error {
- m := gomail.NewMessage( /* gomail.SetEncoding(gomail.Base64) */ )
- m.SetHeader("From", m.FormatAddress(self.username, self.alias))
- m.SetHeader("To", to...)
- m.SetHeader("Subject", subject)
- m.SetBody("text/html", body)
- //m.Attach("/tmp/foo.txt",
- // gomail.Rename("foo.txt"),
- // gomail.SetHeader(map[string][]string{
- // "Content-Disposition": []string{
- // fmt.Sprintf(`attachment; filename="%s"`, mime.QEncoding.Encode("UTF-8", name)),
- // },
- // }),
- //)
- d := gomail.NewDialer(self.host, self.port, self.username, self.password)
- return d.DialAndSend(m)
- }
- type disableMessager struct{}
- func NewDisableMessager() IMessager {
- return &disableMessager{}
- }
- func (self *disableMessager) Send(to []string, subject, body string) error {
- return nil
- }
|