From 480209bf4f86fefdebb19a8ac8d6cf380b877fbd Mon Sep 17 00:00:00 2001 From: Oleksandr Halai Date: Mon, 13 Dec 2021 10:23:33 +0200 Subject: [PATCH] Store last SMTP code and message in Client --- client.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 57bd87e..5666df6 100644 --- a/client.go +++ b/client.go @@ -48,6 +48,10 @@ type Client struct { // Logger for all network activity. DebugWriter io.Writer + + // Last SMTP response + LastCode int + LastMsg string } // 30 seconds was chosen as it's the @@ -207,9 +211,12 @@ func (c *Client) cmd(expectCode int, format string, args ...interface{}) (int, s c.Text.StartResponse(id) defer c.Text.EndResponse(id) code, msg, err := c.Text.ReadResponse(expectCode) + c.LastCode = code + c.LastMsg = msg if err != nil { if protoErr, ok := err.(*textproto.Error); ok { smtpErr := toSMTPErr(protoErr) + c.LastMsg = smtpErr.Message return code, smtpErr.Message, smtpErr } return code, msg, err @@ -457,10 +464,14 @@ func (d *dataCloser) Close() error { } return nil } else { - _, _, err := d.c.Text.ReadResponse(250) + code, msg, err := d.c.Text.ReadResponse(250) + d.c.LastCode = code + d.c.LastMsg = msg if err != nil { if protoErr, ok := err.(*textproto.Error); ok { - return toSMTPErr(protoErr) + smtpErr := toSMTPErr(protoErr) + d.c.LastMsg = smtpErr.Message + return smtpErr } return err }