Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kayrus opened this issue Jun 2, 2022 · 1 comment
Open
Labels

Comments

@kayrus
Copy link
Contributor

kayrus commented Jun 2, 2022

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:

go-smtp/conn.go

Lines 400 to 410 in 608f3c2

if err := c.Session().Mail(from, opts); err != nil {
if smtpErr, ok := err.(*SMTPError); ok {
c.WriteResponse(smtpErr.Code, smtpErr.EnhancedCode, smtpErr.Message)
return
}
c.WriteResponse(451, EnhancedCode{4, 0, 0}, err.Error())
return
}
c.WriteResponse(250, EnhancedCode{2, 0, 0}, fmt.Sprintf("Roger, accepting mail from <%v>", from))
c.fromReceived = true

go-smtp/conn.go

Lines 488 to 497 in 608f3c2

if err := c.Session().Rcpt(recipient); err != nil {
if smtpErr, ok := err.(*SMTPError); ok {
c.WriteResponse(smtpErr.Code, smtpErr.EnhancedCode, smtpErr.Message)
return
}
c.WriteResponse(451, EnhancedCode{4, 0, 0}, err.Error())
return
}
c.recipients = append(c.recipients, recipient)
c.WriteResponse(250, EnhancedCode{2, 0, 0}, fmt.Sprintf("I'll make sure <%v> gets this", recipient))

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())
@kayrus kayrus changed the title Add support to handle a custom response for "Rcpt" method Add support to handle a custom response for "Mail" and "Rcpt" methods Jun 2, 2022
@kayrus kayrus changed the title Add support to handle a custom response for "Mail" and "Rcpt" methods Handle a custom response for "Mail" and "Rcpt" session methods Jun 2, 2022
@kayrus kayrus changed the title Handle a custom response for "Mail" and "Rcpt" session methods Server: handle a custom response for "Mail" and "Rcpt" session methods Jun 3, 2022
@emersion
Copy link
Owner

Not sure this is a desirable feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants