Skip to content

Commit

Permalink
gomail maintainer missing, use active fork
Browse files Browse the repository at this point in the history
  • Loading branch information
yuikns committed Jan 26, 2019
1 parent d80c72e commit a84eb99
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions mail/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@ package mail
import (
"crypto/tls"
"github.com/argcv/stork/config"
"gopkg.in/gomail.v2"
"github.com/go-mail/mail"
"io"
"sync"
"time"
)

// for future update,
// please refer to this example:
// https://github.com/go-gomail/gomail/blob/master/example_test.go#L1
// active fork
// https://github.com/go-mail/mail
type SMTPSession struct {
Dialer *gomail.Dialer
Dialer *mail.Dialer
Config *config.SMTPConfig

isClosed bool
closeSch *SMTPCloseSch
sendCloser gomail.SendCloser
sendCloser mail.SendCloser
}

func (s *SMTPSession) NewMessage() (*SMTPMessage) {
m := &SMTPMessage{
Dialer: s.Dialer,
Config: s.Config,
Message: gomail.NewMessage(),
Message: mail.NewMessage(),

locker: &sync.Mutex{},
ss: s,
Expand All @@ -34,7 +33,7 @@ func (s *SMTPSession) NewMessage() (*SMTPMessage) {
return m
}

func (s *SMTPSession) Dial(f func(gomail.SendCloser) error) error {
func (s *SMTPSession) Dial(f func(mail.SendCloser) error) error {
s.closeSch.Activate()
err := s.closeSch.SafeExec(func() error {
if s.isClosed {
Expand Down Expand Up @@ -64,7 +63,7 @@ func (s *SMTPSession) close() error {
}

func NewSMTPSession(c config.SMTPConfig) (s *SMTPSession, err error) {
dialer := gomail.NewDialer(c.Host, c.Port, c.User, c.Pass)
dialer := mail.NewDialer(c.Host, c.Port, c.User, c.Pass)
if c.Option.InsecureSkipVerify {
dialer.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}
Expand All @@ -82,8 +81,8 @@ func NewSMTPSession(c config.SMTPConfig) (s *SMTPSession, err error) {
}

type SMTPMessage struct {
Dialer *gomail.Dialer
Message *gomail.Message
Dialer *mail.Dialer
Message *mail.Message
Config *config.SMTPConfig

locker *sync.Mutex
Expand Down Expand Up @@ -148,15 +147,15 @@ func (m *SMTPMessage) HtmlBody(body string) *SMTPMessage {

func (m *SMTPMessage) Attach(path string, name ...string) *SMTPMessage {
if len(name) > 0 {
m.Message.Attach(path, gomail.Rename(name[0]))
m.Message.Attach(path, mail.Rename(name[0]))
} else {
m.Message.Attach(path)
}
return m
}

func (m *SMTPMessage) AttachBytes(name string, data []byte) *SMTPMessage {
m.Message.Attach(name, gomail.SetCopyFunc(func(w io.Writer) error {
m.Message.Attach(name, mail.SetCopyFunc(func(w io.Writer) error {
_, err := w.Write(data)
return err
}))
Expand Down Expand Up @@ -187,7 +186,7 @@ func (m *SMTPMessage) AddToHeader(field string, value ...string) *SMTPMessage {
return m
}

func (m *SMTPMessage) AddAlternative(contentType, body string, settings ...gomail.PartSetting) *SMTPMessage {
func (m *SMTPMessage) AddAlternative(contentType, body string, settings ...mail.PartSetting) *SMTPMessage {
m.Message.AddAlternative(contentType, body, settings...)
return m
}
Expand All @@ -205,14 +204,14 @@ func (m *SMTPMessage) FormatAddress(address, name string) string {
// return locker
//}

func (m *SMTPMessage) SetBody(contentType, body string, settings ...gomail.PartSetting) *SMTPMessage {
func (m *SMTPMessage) SetBody(contentType, body string, settings ...mail.PartSetting) *SMTPMessage {
m.Message.SetBody(contentType, body, settings...)
return m
}

func (m *SMTPMessage) Perform() error {
return m.ss.Dial(func(s gomail.SendCloser) (err error) {
err = gomail.Send(s, m.Message)
return m.ss.Dial(func(s mail.SendCloser) (err error) {
err = mail.Send(s, m.Message)
if err != nil {
m.ss.close()
}
Expand Down

0 comments on commit a84eb99

Please sign in to comment.