diff --git a/email.go b/email.go index 48a6bd5..e38dbfe 100644 --- a/email.go +++ b/email.go @@ -56,7 +56,7 @@ type SMTPServer struct { // SMTPClient represents a SMTP Client for send email type SMTPClient struct { - sync.Mutex + mu sync.Mutex Client *smtpClient KeepAlive bool SendTimeout time.Duration @@ -867,29 +867,29 @@ func (server *SMTPServer) Connect() (*SMTPClient, error) { // Reset send RSET command to smtp client func (smtpClient *SMTPClient) Reset() error { - smtpClient.Lock() - defer smtpClient.Unlock() + smtpClient.mu.Lock() + defer smtpClient.mu.Unlock() return smtpClient.Client.reset() } // Noop send NOOP command to smtp client func (smtpClient *SMTPClient) Noop() error { - smtpClient.Lock() - defer smtpClient.Unlock() + smtpClient.mu.Lock() + defer smtpClient.mu.Unlock() return smtpClient.Client.noop() } // Quit send QUIT command to smtp client func (smtpClient *SMTPClient) Quit() error { - smtpClient.Lock() - defer smtpClient.Unlock() + smtpClient.mu.Lock() + defer smtpClient.mu.Unlock() return smtpClient.Client.quit() } // Close closes the connection func (smtpClient *SMTPClient) Close() error { - smtpClient.Lock() - defer smtpClient.Unlock() + smtpClient.mu.Lock() + defer smtpClient.mu.Unlock() return smtpClient.Client.close() } @@ -938,7 +938,6 @@ func send(from string, to []string, msg string, client *SMTPClient) error { checkKeepAlive(client) return errors.New("Mail Error: SMTP Send timed out") } - } } @@ -946,8 +945,8 @@ func send(from string, to []string, msg string, client *SMTPClient) error { } func sendMailProcess(from string, to []string, msg string, c *SMTPClient) error { - c.Lock() - defer c.Unlock() + c.mu.Lock() + defer c.mu.Unlock() cmdArgs := make(map[string]string) diff --git a/email_test.go b/email_test.go index 8200b9b..5cd910d 100644 --- a/email_test.go +++ b/email_test.go @@ -51,13 +51,13 @@ func TestSendRace(t *testing.T) { SetSubject("subject"). SetBody(TextPlain, "body") - // the smtpClient2 has not timeout, so the err, if exists, should be the last message sent in listener service, otherwise is an error + // the smtpClient2 has not timeout err = msg.Send(smtpClient2) - if err != nil && err.Error() != "221 after quit" { + if err != nil { log.Fatalf("couldn't send: %s", err.Error()) } - // the smtpClient send to listener with the last response is after SendTimeout, so when this error is retorned so test is success. + // the smtpClient send to listener with the last response is after SendTimeout, so when this error is returned the test succeed. err = msg.Send(smtpClient) if err != nil && err.Error() != "Mail Error: SMTP Send timed out" { log.Fatalf("couldn't send: %s", err.Error()) @@ -85,7 +85,7 @@ func startService(port int, responses []string, timeout time.Duration) { func respond(conn net.Conn, responses []string, timeout time.Duration) { buf := make([]byte, 1024) for i, resp := range responses { - conn.Write([]byte(resp + "\n")) + write(conn, resp) n, err := conn.Read(buf) if err != nil { log.Println("couldn't read data") @@ -98,10 +98,19 @@ func respond(conn net.Conn, responses []string, timeout time.Duration) { break } } + + // if timeout, sleep for that time, otherwise sent a 250 OK if timeout > 0 { time.Sleep(timeout) + } else { + write(conn, "250 OK") } - conn.Write([]byte(`221 after quit` + "\n")) conn.Close() + fmt.Print("\n\n") +} + +func write(conn net.Conn, command string) { + log.Printf("WRITE:%s", command) + conn.Write([]byte(command + "\n")) }