@@ -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 {
4949type 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
116116func 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 }
0 commit comments