diff --git a/mail.go b/mail.go index 8f59be3..66718d4 100644 --- a/mail.go +++ b/mail.go @@ -57,6 +57,10 @@ func (m *Mail) Rcpt(to string) *smtp.SMTPError { } if action.Type == ActionTypeSyncResponse { + if action.Code == 250 { + m.RcptTo = append(m.RcptTo, to) // add RCPT + } + // note: SMTPError works for 200 as well return &smtp.SMTPError{ Code: action.Code, @@ -73,11 +77,10 @@ func (m *Mail) Rcpt(to string) *smtp.SMTPError { }) } - // add RCPT + // reached for async actions m.RcptTo = append(m.RcptTo, to) - return nil // OK response - + return nil // OK Response } func (m *Mail) Complete() error { diff --git a/mail_test.go b/mail_test.go index d195005..966e7f3 100644 --- a/mail_test.go +++ b/mail_test.go @@ -114,6 +114,47 @@ func TestProcessMailComplaint(t *testing.T) { assert.Equal(t, 0, complaintDelay) } +func TestMailWithOneRcptOkayOtherFail(t *testing.T) { + + mail := NewMail() + + mail.MailFrom = "bounces@example.com" + + err := mail.Rcpt("accept@localhost") + assert.Equal(t, 250, err.Code) + + err = mail.Rcpt("busy@localhost") + assert.Equal(t, 450, err.Code) + + assert.Equal(t, []string{"accept@localhost"}, mail.RcptTo) + + smtpErr := mail.Complete() + assert.Nil(t, smtpErr) + +} + +func TestMailWithOneRcptOkayOneFailAndOtherAsync(t *testing.T) { + + mail := NewMail() + + mail.MailFrom = "bounces@example.com" + + err := mail.Rcpt("accept@localhost") + assert.Equal(t, 250, err.Code) + + err = mail.Rcpt("busy@localhost") + assert.Equal(t, 450, err.Code) + + err = mail.Rcpt("missing+async@localhost") + assert.Nil(t, err) + + assert.Equal(t, []string{"accept@localhost", "missing+async@localhost"}, mail.RcptTo) + + smtpErr := mail.Complete() + assert.Nil(t, smtpErr) + +} + func TestSplitAddress(t *testing.T) { tests := []struct { @@ -165,4 +206,4 @@ func TestSendMailhandler(t *testing.T) { assert.Equal(t, "simulator@localhost", fromGot) assert.Equal(t, []string{"bounces@example.com"}, toGot) -} +} \ No newline at end of file