Skip to content

Commit

Permalink
Fix Quit handling when initial HELO fails
Browse files Browse the repository at this point in the history
Ensure QUIT command can be sent even if initial HELO fails. Added a check to skip retrying HELO if it already failed, allowing for proper closing of the connection. This prevents potential hangs or errors during connection termination.
  • Loading branch information
wneessen committed Oct 23, 2024
1 parent 572751a commit 74fa3f6
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions smtp/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,19 @@ func (c *Client) Noop() error {

// Quit sends the QUIT command and closes the connection to the server.
func (c *Client) Quit() error {
if err := c.hello(); err != nil {
return err
// If we did already tried to send a EHLO/HELO but it failed, we still need to be able to send
// a QUIT to close the connection.
// c.hello() will return the global helloErr of the Client, which will always be set if the HELO
// failed before. Therefore if we already sent a HELO and the error is not nil, we skip another
// EHLO/HELO try
c.mutex.RLock()
didHello := c.didHello
helloErr := c.helloError
c.mutex.RUnlock()
if !didHello || helloErr == nil {
if err := c.hello(); err != nil {
return err
}
}
_, _, err := c.cmd(221, "QUIT")
if err != nil {
Expand Down

0 comments on commit 74fa3f6

Please sign in to comment.