emersion / go-smtp

📤 An SMTP client & server library written in Go
MIT License
1.71k stars 215 forks source link

Server: handle a custom response for "Mail" and "Rcpt" session methods #187

Open kayrus opened 2 years ago

kayrus commented 2 years ago

I can return a custom *SMTPError object, but handleMail and handleRcpt methods won't set the c.fromReceived or add the recipient into the c.recipients.

See below:

https://github.com/emersion/go-smtp/blob/608f3c2840584931f2867716078c43485ea7ef3f/conn.go#L400-L410

https://github.com/emersion/go-smtp/blob/608f3c2840584931f2867716078c43485ea7ef3f/conn.go#L488-L497

I suggest the following fix:

--- a/conn.go
+++ b/conn.go
@@ -419,6 +419,9 @@ func (c *Conn) handleMail(arg string) {
        if err := c.Session().Mail(from, opts); err != nil {
                if smtpErr, ok := err.(*SMTPError); ok {
                        c.WriteResponse(smtpErr.Code, smtpErr.EnhancedCode, smtpErr.Message)
+                       if smtpErr.Code == 250 {
+                               c.fromReceived = true
+                       }
                        return
                }
                c.WriteResponse(451, EnhancedCode{4, 0, 0}, err.Error())
@@ -507,6 +510,9 @@ func (c *Conn) handleRcpt(arg string) {
        if err := c.Session().Rcpt(recipient); err != nil {
                if smtpErr, ok := err.(*SMTPError); ok {
                        c.WriteResponse(smtpErr.Code, smtpErr.EnhancedCode, smtpErr.Message)
+                       if smtpErr.Code == 250 {
+                               c.recipients = append(c.recipients, recipient)
+                       }
                        return
                }
                c.WriteResponse(451, EnhancedCode{4, 0, 0}, err.Error())
emersion commented 7 months ago

Not sure this is a desirable feature.