Skip to content

Commit

Permalink
Commit for v2.1.3 : Timeout type time.Duration instead int
Browse files Browse the repository at this point in the history
-ConnectTimeout and SendTimeout are type time.Duration instead int
-NewSMTPClient() return by default 10 seconds for ConnectTimeout and SendTimeout and default Encryption is EncryptionNone
-Updated readme and example_test
  • Loading branch information
xhit committed Sep 27, 2019
1 parent 9eb4812 commit 8954e75
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ func main() {
server.KeepAlive = true

//Timeout for connect to SMTP Server
server.ConnectTimeout = 10
server.ConnectTimeout = 10 * time.Second

//Timeout for send the data and wait respond
server.SendTimeout = 10
server.SendTimeout = 10 * time.Second

//SMTP client
smtpClient,err :=server.Connect()
Expand Down
30 changes: 14 additions & 16 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type SMTPServer struct {
Encryption encryption
Username string
Password string
ConnectTimeout int
SendTimeout int
ConnectTimeout time.Duration
SendTimeout time.Duration
Host string
Port int
KeepAlive bool
Expand All @@ -49,7 +49,7 @@ type SMTPServer struct {
type SMTPClient struct {
Client *Client
KeepAlive bool
SendTimeout int
SendTimeout time.Duration
}

// part represents the different content parts of an email body.
Expand Down Expand Up @@ -114,7 +114,11 @@ func NewMSG() *Email {

//NewSMTPClient returns the client for send email
func NewSMTPClient() *SMTPServer {
server := &SMTPServer{}
server := &SMTPServer{
Encryption: EncryptionNone,
ConnectTimeout: 10 * time.Second,
SendTimeout: 10 * time.Second,
}
return server
}

Expand Down Expand Up @@ -739,11 +743,8 @@ func (server *SMTPServer) Connect() (*SMTPClient, error) {
var c *Client
var err error

// set the timeout value
timeout := time.Duration(server.ConnectTimeout) * time.Second

// if there is a timeout, setup the channel and do the connect under a goroutine
if timeout != 0 {
// if there is a ConnectTimeout, setup the channel and do the connect under a goroutine
if server.ConnectTimeout != 0 {
smtpConnectChannel = make(chan error, 2)
go func() {
c, err = smtpConnect(server.Host, fmt.Sprintf("%d", server.Port), auth, server.Encryption, new(tls.Config))
Expand All @@ -752,8 +753,8 @@ func (server *SMTPServer) Connect() (*SMTPClient, error) {
}()
}

if timeout == 0 {
// no timeout, just fire the connect
if server.ConnectTimeout == 0 {
// no ConnectTimeout, just fire the connect
c, err = smtpConnect(server.Host, fmt.Sprintf("%d", server.Port), auth, server.Encryption, new(tls.Config))
} else {
// get the connect result or timeout result, which ever happens first
Expand All @@ -762,7 +763,7 @@ func (server *SMTPServer) Connect() (*SMTPClient, error) {
if err != nil {
return nil, errors.New(err.Error())
}
case <-time.After(timeout):
case <-time.After(server.ConnectTimeout):
return nil, errors.New("Mail Error: SMTP Connection timed out")
}
}
Expand All @@ -784,9 +785,6 @@ func send(from string, to []string, msg string, smtpClient *SMTPClient) error {
if smtpClient.Client != nil {
var smtpSendChannel chan error

// set the timeout value
timeout := time.Duration(smtpClient.SendTimeout) * time.Second

smtpSendChannel = make(chan error, 1)

go func(c *Client) {
Expand Down Expand Up @@ -832,7 +830,7 @@ func send(from string, to []string, msg string, smtpClient *SMTPClient) error {
case sendError := <-smtpSendChannel:
checkKeepAlive(smtpClient)
return sendError
case <-time.After(timeout):
case <-time.After(smtpClient.SendTimeout):
checkKeepAlive(smtpClient)
return errors.New("Mail Error: SMTP Send timed out")
}
Expand Down
5 changes: 3 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mail

import (
"testing"
"time"
)

//Some variables to connect and the body
Expand All @@ -23,8 +24,8 @@ var (
username = "test@example.com"
password = "examplepass"
encryptionType = EncryptionTLS
connectTimeout = 10
sendTimeout = 10
connectTimeout = 10 * time.Second
sendTimeout = 10 * time.Second
)

//TestSendMailWithAttachment send a simple html email
Expand Down

0 comments on commit 8954e75

Please sign in to comment.