@@ -10,6 +10,7 @@ import (
10
10
"net/textproto"
11
11
"strconv"
12
12
"strings"
13
+ "sync"
13
14
"time"
14
15
15
16
"github.com/toorop/go-dkim"
@@ -55,6 +56,7 @@ type SMTPServer struct {
55
56
56
57
// SMTPClient represents a SMTP Client for send email
57
58
type SMTPClient struct {
59
+ mu sync.Mutex
58
60
Client * smtpClient
59
61
KeepAlive bool
60
62
SendTimeout time.Duration
@@ -865,21 +867,29 @@ func (server *SMTPServer) Connect() (*SMTPClient, error) {
865
867
866
868
// Reset send RSET command to smtp client
867
869
func (smtpClient * SMTPClient ) Reset () error {
870
+ smtpClient .mu .Lock ()
871
+ defer smtpClient .mu .Unlock ()
868
872
return smtpClient .Client .reset ()
869
873
}
870
874
871
875
// Noop send NOOP command to smtp client
872
876
func (smtpClient * SMTPClient ) Noop () error {
877
+ smtpClient .mu .Lock ()
878
+ defer smtpClient .mu .Unlock ()
873
879
return smtpClient .Client .noop ()
874
880
}
875
881
876
882
// Quit send QUIT command to smtp client
877
883
func (smtpClient * SMTPClient ) Quit () error {
884
+ smtpClient .mu .Lock ()
885
+ defer smtpClient .mu .Unlock ()
878
886
return smtpClient .Client .quit ()
879
887
}
880
888
881
889
// Close closes the connection
882
890
func (smtpClient * SMTPClient ) Close () error {
891
+ smtpClient .mu .Lock ()
892
+ defer smtpClient .mu .Unlock ()
883
893
return smtpClient .Client .close ()
884
894
}
885
895
@@ -909,14 +919,14 @@ func send(from string, to []string, msg string, client *SMTPClient) error {
909
919
if client .SendTimeout != 0 {
910
920
smtpSendChannel = make (chan error , 1 )
911
921
912
- go func (from string , to []string , msg string , c * smtpClient ) {
913
- smtpSendChannel <- sendMailProcess (from , to , msg , c )
914
- }(from , to , msg , client . Client )
922
+ go func (from string , to []string , msg string , client * SMTPClient ) {
923
+ smtpSendChannel <- sendMailProcess (from , to , msg , client )
924
+ }(from , to , msg , client )
915
925
}
916
926
917
927
if client .SendTimeout == 0 {
918
928
// no SendTimeout, just fire the sendMailProcess
919
- return sendMailProcess (from , to , msg , client . Client )
929
+ return sendMailProcess (from , to , msg , client )
920
930
}
921
931
922
932
// get the send result or timeout result, which ever happens first
@@ -928,35 +938,36 @@ func send(from string, to []string, msg string, client *SMTPClient) error {
928
938
checkKeepAlive (client )
929
939
return errors .New ("Mail Error: SMTP Send timed out" )
930
940
}
931
-
932
941
}
933
942
}
934
943
935
944
return errors .New ("Mail Error: No SMTP Client Provided" )
936
945
}
937
946
938
- func sendMailProcess (from string , to []string , msg string , c * smtpClient ) error {
947
+ func sendMailProcess (from string , to []string , msg string , c * SMTPClient ) error {
948
+ c .mu .Lock ()
949
+ defer c .mu .Unlock ()
939
950
940
951
cmdArgs := make (map [string ]string )
941
952
942
- if _ , ok := c .ext ["SIZE" ]; ok {
953
+ if _ , ok := c .Client . ext ["SIZE" ]; ok {
943
954
cmdArgs ["SIZE" ] = strconv .Itoa (len (msg ))
944
955
}
945
956
946
957
// Set the sender
947
- if err := c .mail (from , cmdArgs ); err != nil {
958
+ if err := c .Client . mail (from , cmdArgs ); err != nil {
948
959
return err
949
960
}
950
961
951
962
// Set the recipients
952
963
for _ , address := range to {
953
- if err := c .rcpt (address ); err != nil {
964
+ if err := c .Client . rcpt (address ); err != nil {
954
965
return err
955
966
}
956
967
}
957
968
958
969
// Send the data command
959
- w , err := c .data ()
970
+ w , err := c .Client . data ()
960
971
if err != nil {
961
972
return err
962
973
}
@@ -978,9 +989,9 @@ func sendMailProcess(from string, to []string, msg string, c *smtpClient) error
978
989
// check if keepAlive for close or reset
979
990
func checkKeepAlive (client * SMTPClient ) {
980
991
if client .KeepAlive {
981
- client .Client . reset ()
992
+ client .Reset ()
982
993
} else {
983
- client .Client . quit ()
984
- client .Client . close ()
994
+ client .Quit ()
995
+ client .Close ()
985
996
}
986
997
}
0 commit comments