Skip to content

Commit

Permalink
Read server response on DATA command
Browse files Browse the repository at this point in the history
  • Loading branch information
kayrus committed Jun 3, 2022
1 parent 608f3c2 commit 35de77e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
49 changes: 28 additions & 21 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,34 +438,39 @@ func (d *dataCloser) Close() error {
d.c.conn.SetDeadline(time.Now().Add(d.c.SubmissionTimeout))
defer d.c.conn.SetDeadline(time.Time{})

expectedResponses := len(d.c.rcpts)
if d.c.lmtp {
for expectedResponses > 0 {
rcpt := d.c.rcpts[len(d.c.rcpts)-expectedResponses]
if _, _, err := d.c.Text.ReadResponse(250); err != nil {
if protoErr, ok := err.(*textproto.Error); ok {
if d.statusCb != nil {
d.statusCb(rcpt, toSMTPErr(protoErr))
}
} else {
for _, rcpt := range d.c.rcpts {
code, msg, err := d.c.Text.ReadResponse(250)
resp := &textproto.Error{
Code: code,
Msg: msg,
}
if err != nil {
var ok bool
resp, ok = err.(*textproto.Error)
if !ok {
return err
}
} else if d.statusCb != nil {
d.statusCb(rcpt, nil)
}
expectedResponses--
}
return nil
} else {
_, _, err := d.c.Text.ReadResponse(250)
if err != nil {
if protoErr, ok := err.(*textproto.Error); ok {
return toSMTPErr(protoErr)
if d.statusCb != nil {
d.statusCb(rcpt, toSMTPErr(resp))
}
return err
}
return nil
}

code, msg, err := d.c.Text.ReadResponse(250)
resp := &textproto.Error{
Code: code,
Msg: msg,
}
if err != nil {
if err, ok := err.(*textproto.Error); ok {
return toSMTPErr(err)
}
return err
}
return toSMTPErr(resp)
}

// Data issues a DATA command to the server and returns a writer that
Expand Down Expand Up @@ -537,7 +542,9 @@ func (c *Client) SendMail(from string, to []string, r io.Reader) error {
}
err = w.Close()
if err != nil {
return err
if e, ok := err.(*SMTPError); !ok || e.Code != 250 {
return err
}
}
return c.Quit()
}
Expand Down
12 changes: 9 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ Goodbye.`
t.Fatalf("Data write failed: %s", err)
}
if err := w.Close(); err != nil {
t.Fatalf("Bad data response: %s", err)
if e, ok := err.(*SMTPError); !ok || e.Code != 250 {
t.Fatalf("Bad data response: %s", err)
}
}

if err := c.Quit(); err != nil {
Expand Down Expand Up @@ -909,7 +911,9 @@ Goodbye.`
t.Fatalf("Data write failed: %s", err)
}
if err := w.Close(); err != nil {
t.Fatalf("Bad data response: %s", err)
if e, ok := err.(*SMTPError); !ok || e.Code != 250 {
t.Fatalf("Bad data response: %s", err)
}
}

if !reflect.DeepEqual(rcpts, []string{"[email protected]", "[email protected]"}) {
Expand All @@ -920,7 +924,9 @@ Goodbye.`
t.Fatalf("Wrong amount of status callback calls: %v", len(errors))
}
if errors[0] != nil {
t.Fatalf("Unexpected error status for the first recipient: %v", errors[0])
if errors[0].Code != 250 {
t.Fatalf("Unexpected error status for the first recipient: %v", errors[0])
}
}
if errors[1] == nil {
t.Fatalf("Unexpected success status for the second recipient")
Expand Down

0 comments on commit 35de77e

Please sign in to comment.