Skip to content

Commit 8954e75

Browse files
committed
Commit for v2.1.3 : Timeout type time.Duration instead int
-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
1 parent 9eb4812 commit 8954e75

3 files changed

Lines changed: 19 additions & 20 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ func main() {
5555
server.KeepAlive = true
5656

5757
//Timeout for connect to SMTP Server
58-
server.ConnectTimeout = 10
58+
server.ConnectTimeout = 10 * time.Second
5959

6060
//Timeout for send the data and wait respond
61-
server.SendTimeout = 10
61+
server.SendTimeout = 10 * time.Second
6262

6363
//SMTP client
6464
smtpClient,err :=server.Connect()

email.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ type SMTPServer struct {
3838
Encryption encryption
3939
Username string
4040
Password string
41-
ConnectTimeout int
42-
SendTimeout int
41+
ConnectTimeout time.Duration
42+
SendTimeout time.Duration
4343
Host string
4444
Port int
4545
KeepAlive bool
@@ -49,7 +49,7 @@ type SMTPServer struct {
4949
type SMTPClient struct {
5050
Client *Client
5151
KeepAlive bool
52-
SendTimeout int
52+
SendTimeout time.Duration
5353
}
5454

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

115115
//NewSMTPClient returns the client for send email
116116
func NewSMTPClient() *SMTPServer {
117-
server := &SMTPServer{}
117+
server := &SMTPServer{
118+
Encryption: EncryptionNone,
119+
ConnectTimeout: 10 * time.Second,
120+
SendTimeout: 10 * time.Second,
121+
}
118122
return server
119123
}
120124

@@ -739,11 +743,8 @@ func (server *SMTPServer) Connect() (*SMTPClient, error) {
739743
var c *Client
740744
var err error
741745

742-
// set the timeout value
743-
timeout := time.Duration(server.ConnectTimeout) * time.Second
744-
745-
// if there is a timeout, setup the channel and do the connect under a goroutine
746-
if timeout != 0 {
746+
// if there is a ConnectTimeout, setup the channel and do the connect under a goroutine
747+
if server.ConnectTimeout != 0 {
747748
smtpConnectChannel = make(chan error, 2)
748749
go func() {
749750
c, err = smtpConnect(server.Host, fmt.Sprintf("%d", server.Port), auth, server.Encryption, new(tls.Config))
@@ -752,8 +753,8 @@ func (server *SMTPServer) Connect() (*SMTPClient, error) {
752753
}()
753754
}
754755

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

787-
// set the timeout value
788-
timeout := time.Duration(smtpClient.SendTimeout) * time.Second
789-
790788
smtpSendChannel = make(chan error, 1)
791789

792790
go func(c *Client) {
@@ -832,7 +830,7 @@ func send(from string, to []string, msg string, smtpClient *SMTPClient) error {
832830
case sendError := <-smtpSendChannel:
833831
checkKeepAlive(smtpClient)
834832
return sendError
835-
case <-time.After(timeout):
833+
case <-time.After(smtpClient.SendTimeout):
836834
checkKeepAlive(smtpClient)
837835
return errors.New("Mail Error: SMTP Send timed out")
838836
}

example_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mail
22

33
import (
44
"testing"
5+
"time"
56
)
67

78
//Some variables to connect and the body
@@ -23,8 +24,8 @@ var (
2324
username = "test@example.com"
2425
password = "examplepass"
2526
encryptionType = EncryptionTLS
26-
connectTimeout = 10
27-
sendTimeout = 10
27+
connectTimeout = 10 * time.Second
28+
sendTimeout = 10 * time.Second
2829
)
2930

3031
//TestSendMailWithAttachment send a simple html email

0 commit comments

Comments
 (0)