Skip to content

Commit

Permalink
client: ensure dataCloser is only closed twice
Browse files Browse the repository at this point in the history
Otherwise we end up writing the dot plus CRLF sequence multiple
times.
  • Loading branch information
emersion committed Oct 18, 2022
1 parent 7c94d3c commit e784ad0
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,14 @@ type dataCloser struct {
c *Client
io.WriteCloser
statusCb func(rcpt string, status *SMTPError)
closed bool
}

func (d *dataCloser) Close() error {
if d.closed {
return fmt.Errorf("smtp: data writer closed twice")
}

if err := d.WriteCloser.Close(); err != nil {
return err
}
Expand All @@ -457,7 +462,6 @@ func (d *dataCloser) Close() error {
}
expectedResponses--
}
return nil
} else {
_, _, err := d.c.Text.ReadResponse(250)
if err != nil {
Expand All @@ -466,8 +470,10 @@ func (d *dataCloser) Close() error {
}
return err
}
return nil
}

d.closed = true
return nil
}

// Data issues a DATA command to the server and returns a writer that
Expand All @@ -481,7 +487,7 @@ func (c *Client) Data() (io.WriteCloser, error) {
if err != nil {
return nil, err
}
return &dataCloser{c, c.Text.DotWriter(), nil}, nil
return &dataCloser{c: c, WriteCloser: c.Text.DotWriter()}, nil
}

// LMTPData is the LMTP-specific version of the Data method. It accepts a callback
Expand All @@ -501,7 +507,7 @@ func (c *Client) LMTPData(statusCb func(rcpt string, status *SMTPError)) (io.Wri
if err != nil {
return nil, err
}
return &dataCloser{c, c.Text.DotWriter(), statusCb}, nil
return &dataCloser{c: c, WriteCloser: c.Text.DotWriter(), statusCb: statusCb}, nil
}

// SendMail will use an existing connection to send an email from
Expand Down

0 comments on commit e784ad0

Please sign in to comment.