@@ -52,6 +52,9 @@ type SMTPServer struct {
52
52
Port int
53
53
KeepAlive bool
54
54
TLSConfig * tls.Config
55
+
56
+ // use custom dialer
57
+ CustomConn net.Conn
55
58
}
56
59
57
60
// SMTPClient represents a SMTP Client for send email
@@ -711,27 +714,30 @@ func (email *Email) SendEnvelopeFrom(from string, client *SMTPClient) error {
711
714
}
712
715
713
716
// dial connects to the smtp server with the request encryption type
714
- func dial (host string , port string , encryption Encryption , config * tls.Config ) (* smtpClient , error ) {
717
+ func dial (customConn net. Conn , host string , port string , encryption Encryption , config * tls.Config ) (* smtpClient , error ) {
715
718
var conn net.Conn
716
719
var err error
720
+ var c * smtpClient
717
721
718
- address := host + ":" + port
719
-
720
- // do the actual dial
721
- switch encryption {
722
- // TODO: Remove EncryptionSSL check before launch v3
723
- case EncryptionSSL , EncryptionSSLTLS :
724
- conn , err = tls .Dial ("tcp" , address , config )
725
- default :
726
- conn , err = net .Dial ("tcp" , address )
727
- }
722
+ if customConn != nil {
723
+ conn = customConn
724
+ } else {
725
+ address := host + ":" + port
726
+ // do the actual dial
727
+ switch encryption {
728
+ // TODO: Remove EncryptionSSL check before launch v3
729
+ case EncryptionSSL , EncryptionSSLTLS :
730
+ conn , err = tls .Dial ("tcp" , address , config )
731
+ default :
732
+ conn , err = net .Dial ("tcp" , address )
733
+ }
728
734
729
- if err != nil {
730
- return nil , errors .New ("Mail Error on dialing with encryption type " + encryption .String () + ": " + err .Error ())
735
+ if err != nil {
736
+ return nil , errors .New ("Mail Error on dialing with encryption type " + encryption .String () + ": " + err .Error ())
737
+ }
731
738
}
732
739
733
- c , err := newClient (conn , host )
734
-
740
+ c , err = newClient (conn , host )
735
741
if err != nil {
736
742
return nil , fmt .Errorf ("Mail Error on smtp dial: %w" , err )
737
743
}
@@ -741,9 +747,9 @@ func dial(host string, port string, encryption Encryption, config *tls.Config) (
741
747
742
748
// smtpConnect connects to the smtp server and starts TLS and passes auth
743
749
// if necessary
744
- func smtpConnect (host , port , helo string , encryption Encryption , config * tls.Config ) (* smtpClient , error ) {
750
+ func smtpConnect (customConn net. Conn , host , port , helo string , encryption Encryption , config * tls.Config ) (* smtpClient , error ) {
745
751
// connect to the mail server
746
- c , err := dial (host , port , encryption , config )
752
+ c , err := dial (customConn , host , port , encryption , config )
747
753
748
754
if err != nil {
749
755
return nil , err
@@ -837,7 +843,7 @@ func (server *SMTPServer) Connect() (*SMTPClient, error) {
837
843
if server .ConnectTimeout != 0 {
838
844
smtpConnectChannel = make (chan error , 2 )
839
845
go func () {
840
- c , err = smtpConnect (server .Host , fmt .Sprintf ("%d" , server .Port ), server .Helo , server .Encryption , tlsConfig )
846
+ c , err = smtpConnect (server .CustomConn , server . Host , fmt .Sprintf ("%d" , server .Port ), server .Helo , server .Encryption , tlsConfig )
841
847
// send the result
842
848
smtpConnectChannel <- err
843
849
}()
@@ -852,7 +858,7 @@ func (server *SMTPServer) Connect() (*SMTPClient, error) {
852
858
}
853
859
} else {
854
860
// no ConnectTimeout, just fire the connect
855
- c , err = smtpConnect (server .Host , fmt .Sprintf ("%d" , server .Port ), server .Helo , server .Encryption , tlsConfig )
861
+ c , err = smtpConnect (server .CustomConn , server . Host , fmt .Sprintf ("%d" , server .Port ), server .Helo , server .Encryption , tlsConfig )
856
862
if err != nil {
857
863
return nil , err
858
864
}
0 commit comments