Skip to content

Commit

Permalink
Introduce RcptOptions
Browse files Browse the repository at this point in the history
Some SMTP extensions add new RCPT parameters. Add a struct to be
able to support these.
  • Loading branch information
emersion committed Aug 14, 2023
1 parent d576c01 commit db55b9c
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Session interface {
// Set return path for currently processed message.
Mail(from string, opts *MailOptions) error
// Add recipient for currently processed message.
Rcpt(to string) error
Rcpt(to string, opts *RcptOptions) error
// Set currently processed message contents and send it.
//
// r must be consumed before Data returns.
Expand Down
4 changes: 2 additions & 2 deletions backendutil/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ func (s *transformSession) Mail(from string, opts *smtp.MailOptions) error {
return s.Session.Mail(from, opts)
}

func (s *transformSession) Rcpt(to string) error {
func (s *transformSession) Rcpt(to string, opts *smtp.RcptOptions) error {
if s.be.TransformRcpt != nil {
var err error
to, err = s.be.TransformRcpt(to)
if err != nil {
return err
}
}
return s.Session.Rcpt(to)
return s.Session.Rcpt(to, opts)
}

func (s *transformSession) Data(r io.Reader) error {
Expand Down
2 changes: 1 addition & 1 deletion backendutil/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *session) Mail(from string, opts *smtp.MailOptions) error {
return nil
}

func (s *session) Rcpt(to string) error {
func (s *session) Rcpt(to string, opts *smtp.RcptOptions) error {
s.msg.To = append(s.msg.To, to)
return nil
}
Expand Down
7 changes: 5 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,11 @@ func (c *Client) Mail(from string, opts *MailOptions) error {
// A call to Rcpt must be preceded by a call to Mail and may be followed by
// a Data call or another Rcpt call.
//
// If opts is not nil, RCPT arguments provided in the structure will be added
// to the command. Handling of unsupported options depends on the extension.
//
// If server returns an error, it will be of type *SMTPError.
func (c *Client) Rcpt(to string) error {
func (c *Client) Rcpt(to string, opts *RcptOptions) error {
if err := validateLine(to); err != nil {
return err
}
Expand Down Expand Up @@ -531,7 +534,7 @@ func (c *Client) SendMail(from string, to []string, r io.Reader) error {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
if err = c.Rcpt(addr, nil); err != nil {
return err
}
}
Expand Down
10 changes: 5 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestBasic(t *testing.T) {
t.Fatalf("AUTH failed: %s", err)
}

if err := c.Rcpt("[email protected]>\r\nDATA\r\nInjected message body\r\n.\r\nQUIT\r\n"); err == nil {
if err := c.Rcpt("[email protected]>\r\nDATA\r\nInjected message body\r\n.\r\nQUIT\r\n", nil); err == nil {
t.Fatalf("RCPT should have failed due to a message injection attempt")
}
if err := c.Mail("[email protected]>\r\nDATA\r\nAnother injected message body\r\n.\r\nQUIT\r\n", nil); err == nil {
Expand All @@ -129,7 +129,7 @@ func TestBasic(t *testing.T) {
if err := c.Mail("[email protected]", nil); err != nil {
t.Fatalf("MAIL failed: %s", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
if err := c.Rcpt("[email protected]", nil); err != nil {
t.Fatalf("RCPT failed: %s", err)
}
msg := `From: [email protected]
Expand Down Expand Up @@ -797,7 +797,7 @@ func TestLMTP(t *testing.T) {
if err := c.Mail("[email protected]", nil); err != nil {
t.Fatalf("MAIL failed: %s", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
if err := c.Rcpt("[email protected]", nil); err != nil {
t.Fatalf("RCPT failed: %s", err)
}
msg := `From: [email protected]
Expand Down Expand Up @@ -882,10 +882,10 @@ func TestLMTPData(t *testing.T) {
if err := c.Mail("[email protected]", nil); err != nil {
t.Fatalf("MAIL failed: %s", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
if err := c.Rcpt("[email protected]", nil); err != nil {
t.Fatalf("RCPT failed: %s", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
if err := c.Rcpt("[email protected]", nil); err != nil {
t.Fatalf("RCPT failed: %s", err)
}
msg := `From: [email protected]
Expand Down
2 changes: 1 addition & 1 deletion cmd/smtp-debug-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s *session) Mail(from string, opts *smtp.MailOptions) error {
return nil
}

func (s *session) Rcpt(to string) error {
func (s *session) Rcpt(to string, opts *smtp.RcptOptions) error {
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ func (c *Conn) handleRcpt(arg string) {
return
}

if err := c.Session().Rcpt(recipient); err != nil {
opts := &RcptOptions{}
if err := c.Session().Rcpt(recipient, opts); err != nil {
if smtpErr, ok := err.(*SMTPError); ok {
c.writeResponse(smtpErr.Code, smtpErr.EnhancedCode, smtpErr.Message)
return
Expand Down
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func ExampleDial() {
if err := c.Mail("[email protected]", nil); err != nil {
log.Fatal(err)
}
if err := c.Rcpt("[email protected]"); err != nil {
if err := c.Rcpt("[email protected]", nil); err != nil {
log.Fatal(err)
}

Expand Down Expand Up @@ -113,7 +113,7 @@ func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
return nil
}

func (s *Session) Rcpt(to string) error {
func (s *Session) Rcpt(to string, opts *smtp.RcptOptions) error {
log.Println("Rcpt to:", to)
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *session) Mail(from string, opts *smtp.MailOptions) error {
return nil
}

func (s *session) Rcpt(to string) error {
func (s *session) Rcpt(to string, opts *smtp.RcptOptions) error {
s.msg.To = append(s.msg.To, to)
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ const (
BodyBinaryMIME BodyType = "BINARYMIME"
)

// MailOptions contains custom arguments that were
// passed as an argument to the MAIL command.
// MailOptions contains parameters for the MAIL command.
type MailOptions struct {
// Value of BODY= argument, 7BIT, 8BITMIME or BINARYMIME.
Body BodyType
Expand All @@ -52,3 +51,6 @@ type MailOptions struct {
// Defined in RFC 4954.
Auth *string
}

// RcptOptions contains parameters for the RCPT command.
type RcptOptions struct{}

0 comments on commit db55b9c

Please sign in to comment.