Skip to content

Commit

Permalink
Merge pull request #167 from wneessen/feature/166_no-easy-way-to-dete…
Browse files Browse the repository at this point in the history
…rmine-if-e-mail-was-send

Add delivery status indication for messages
  • Loading branch information
wneessen authored Jan 23, 2024
2 parents e4927fd + 6746af4 commit 99dd296
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions client_119.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (c *Client) Send(ml ...*Msg) error {
errs = append(errs, se)
continue
}
m.isDelivered = true

if err := w.Close(); err != nil {
se := &SendError{Reason: ErrSMTPDataClose, errlist: []error{err}, isTemp: isTempError(err)}
Expand Down
1 change: 1 addition & 0 deletions client_120.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (c *Client) Send(ml ...*Msg) (rerr error) {
rerr = errors.Join(rerr, m.sendError)
continue
}
m.isDelivered = true

if err := w.Close(); err != nil {
m.sendError = &SendError{Reason: ErrSMTPDataClose, errlist: []error{err}, isTemp: isTempError(err)}
Expand Down
7 changes: 6 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,9 @@ func TestClient_DialSendClose(t *testing.T) {
if err := c.Close(); err != nil {
t.Errorf("Close() failed: %s", err)
}
if !m.IsDelivered() {
t.Errorf("message should be delivered but is indicated no to")
}
}

// TestClient_DialAndSendWithContext tests the DialAndSendWithContext() method of Client
Expand Down Expand Up @@ -1134,7 +1137,9 @@ func TestClient_DialAndSendWithContext_withSendError(t *testing.T) {
}
if se.IsTemp() {
t.Errorf("expected permanent error but IsTemp() returned true")
return
}
if m.IsDelivered() {
t.Errorf("message is indicated to be delivered but shouldn't")
}
}

Expand Down
8 changes: 8 additions & 0 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ type Msg struct {
// genHeader is a slice of strings that the different generic mail Header fields
genHeader map[Header][]string

// isDelivered signals if a message has been delivered or not
isDelivered bool

// middlewares is the list of middlewares to apply to the Msg before sending in FIFO order
middlewares []Middleware

Expand Down Expand Up @@ -507,6 +510,11 @@ func (m *Msg) SetUserAgent(a string) {
m.SetGenHeader(HeaderXMailer, a)
}

// IsDelivered will return true if the Msg has been successfully delivered
func (m *Msg) IsDelivered() bool {
return m.isDelivered
}

// RequestMDNTo adds the Disposition-Notification-To header to request a MDN from the receiving end
// as described in RFC8098. It allows to provide a list recipient addresses.
// Address validation is performed
Expand Down

0 comments on commit 99dd296

Please sign in to comment.