package providers import ( "crypto/tls" "git.wenlab.co/joe/beaconfire" gomail "gopkg.in/gomail.v2" ) const SMTP_NAME = "smtp" type OptionsSmtp struct { Host string Port int InsecureSkipVerify bool Username string Password string To []string } type smtp struct { opt *OptionsSmtp } var _ beaconfire.BeaconFire = &smtp{} func NewSmtp(opt *OptionsSmtp) *smtp { return &smtp{ opt: opt, } } func (c *smtp) Name() string { return SMTP_NAME } func (c *smtp) Send(bp *beaconfire.BeaconParam) error { if len(c.opt.To) <= 0 { return beaconfire.ErrNoReceiver } mail := gomail.NewMessage() mail.SetHeader("From", c.opt.Username) mail.SetHeader("To", c.opt.To...) mail.SetHeader("Subject", bp.Title) inHtml := true // format == "html" content := bp.FormatHTML() if inHtml { mail.SetBody("text/html", content) } else { mail.SetBody("text/plain", bp.Content) } var d *gomail.Dialer if c.opt.Username != "" && c.opt.Password != "" { d = gomail.NewDialer(c.opt.Host, c.opt.Port, c.opt.Username, c.opt.Password) } else { d = &gomail.Dialer{Host: c.opt.Host, Port: c.opt.Port} } if c.opt.InsecureSkipVerify { d.TLSConfig = &tls.Config{InsecureSkipVerify: true} } return d.DialAndSend(mail) }