From 0b622368baa7c73731529e38ff0a5475a7a4328c Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Tue, 23 Jan 2024 11:01:08 +0100 Subject: [PATCH 1/2] #166: Add delivery status indication for messages This update introduces a new property, `isDelivered`, in the message struct to track if a message has been successfully sent or not. Additionally, this also includes a new helper function `IsDelivered` to easily retrieve the status. This feature enhances the tracking capabilities of outgoing messages across different client files. --- client_119.go | 1 + client_120.go | 1 + msg.go | 8 ++++++++ 3 files changed, 10 insertions(+) diff --git a/client_119.go b/client_119.go index 3165a2d6..df09e18b 100644 --- a/client_119.go +++ b/client_119.go @@ -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)} diff --git a/client_120.go b/client_120.go index 69eb5b33..1c0a5be5 100644 --- a/client_120.go +++ b/client_120.go @@ -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)} diff --git a/msg.go b/msg.go index a3ce3dab..c97fc01b 100644 --- a/msg.go +++ b/msg.go @@ -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 @@ -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 From 6746af4605b6a32b8daa92ff1505415b802b6203 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Tue, 23 Jan 2024 11:21:56 +0100 Subject: [PATCH 2/2] Update client tests for message delivery indication This commit adds checks in the testing functions to verify the successful implementation of message delivery status. A new attribute `isDelivered` is now being validated in our tests, reinforcing the message tracking system and enhancing the overall reliability of our application. --- client_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/client_test.go b/client_test.go index afc5743a..8c35e192 100644 --- a/client_test.go +++ b/client_test.go @@ -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 @@ -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") } }