| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- 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
- }
- 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(bp.To) <= 0 {
- return beaconfire.ErrNoReceiver
- }
- mail := gomail.NewMessage()
- mail.SetHeader("From", bp.From)
- mail.SetHeader("To", bp.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)
- }
|