Skip to content

Commit

Permalink
smtp support starttls
Browse files Browse the repository at this point in the history
  • Loading branch information
UlricQin committed Jul 26, 2019
1 parent 59b5896 commit 6d8aa8d
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type SMTP struct {
Port int
MaxMailContentLength int
SSL bool
StartTLS bool
}

type Mail struct {
Expand Down Expand Up @@ -45,7 +46,7 @@ func (m Mail) Validate(max int) error {
return nil
}

func NewSMTP(fromMail, fromName, username, password, host string, port int, ssl bool) *SMTP {
func NewSMTP(fromMail, fromName, username, password, host string, port int, ssl, startTLS bool) *SMTP {
return &SMTP{
FromMail: fromMail,
FromName: fromName,
Expand All @@ -56,6 +57,7 @@ func NewSMTP(fromMail, fromName, username, password, host string, port int, ssl
Port: port,
MaxMailContentLength: 102400,
SSL: ssl,
StartTLS: startTLS,
}
}

Expand Down Expand Up @@ -91,6 +93,7 @@ func (s *SMTP) Send(mail Mail) error {
mail.Tos,
[]byte(message),
s.SSL,
s.StartTLS,
)

if err != nil {
Expand All @@ -100,16 +103,17 @@ func (s *SMTP) Send(mail Mail) error {
return nil
}

func send(addr string, auth smtp.Auth, from string, tos []string, body []byte, ssl bool) error {
func send(addr string, auth smtp.Auth, from string, tos []string, body []byte, ssl, startTLS bool) error {
host := strings.Split(addr, ":")[0]

tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: host,
}

var c *smtp.Client
var err error
if ssl {
host := strings.Split(addr, ":")[0]
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: host,
}

conn, err := tls.Dial("tcp", addr, tlsconfig)
if err != nil {
return fmt.Errorf("tls dial %s fail: %s", addr, err)
Expand All @@ -124,6 +128,13 @@ func send(addr string, auth smtp.Auth, from string, tos []string, body []byte, s
if err != nil {
return fmt.Errorf("dial %s fail: %v", addr, err)
}

if startTLS {
err = c.StartTLS(tlsconfig)
if err != nil {
return fmt.Errorf("cannot starttls: addr: %s, err: %v", addr, err)
}
}
}

defer c.Close()
Expand Down

0 comments on commit 6d8aa8d

Please sign in to comment.