Skip to content

Commit db55b9c

Browse files
committed
Introduce RcptOptions
Some SMTP extensions add new RCPT parameters. Add a struct to be able to support these.
1 parent d576c01 commit db55b9c

File tree

10 files changed

+24
-18
lines changed

10 files changed

+24
-18
lines changed

backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Session interface {
4343
// Set return path for currently processed message.
4444
Mail(from string, opts *MailOptions) error
4545
// Add recipient for currently processed message.
46-
Rcpt(to string) error
46+
Rcpt(to string, opts *RcptOptions) error
4747
// Set currently processed message contents and send it.
4848
//
4949
// r must be consumed before Data returns.

backendutil/transform.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ func (s *transformSession) Mail(from string, opts *smtp.MailOptions) error {
4848
return s.Session.Mail(from, opts)
4949
}
5050

51-
func (s *transformSession) Rcpt(to string) error {
51+
func (s *transformSession) Rcpt(to string, opts *smtp.RcptOptions) error {
5252
if s.be.TransformRcpt != nil {
5353
var err error
5454
to, err = s.be.TransformRcpt(to)
5555
if err != nil {
5656
return err
5757
}
5858
}
59-
return s.Session.Rcpt(to)
59+
return s.Session.Rcpt(to, opts)
6060
}
6161

6262
func (s *transformSession) Data(r io.Reader) error {

backendutil/transform_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (s *session) Mail(from string, opts *smtp.MailOptions) error {
6565
return nil
6666
}
6767

68-
func (s *session) Rcpt(to string) error {
68+
func (s *session) Rcpt(to string, opts *smtp.RcptOptions) error {
6969
s.msg.To = append(s.msg.To, to)
7070
return nil
7171
}

client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,11 @@ func (c *Client) Mail(from string, opts *MailOptions) error {
414414
// A call to Rcpt must be preceded by a call to Mail and may be followed by
415415
// a Data call or another Rcpt call.
416416
//
417+
// If opts is not nil, RCPT arguments provided in the structure will be added
418+
// to the command. Handling of unsupported options depends on the extension.
419+
//
417420
// If server returns an error, it will be of type *SMTPError.
418-
func (c *Client) Rcpt(to string) error {
421+
func (c *Client) Rcpt(to string, opts *RcptOptions) error {
419422
if err := validateLine(to); err != nil {
420423
return err
421424
}
@@ -531,7 +534,7 @@ func (c *Client) SendMail(from string, to []string, r io.Reader) error {
531534
return err
532535
}
533536
for _, addr := range to {
534-
if err = c.Rcpt(addr); err != nil {
537+
if err = c.Rcpt(addr, nil); err != nil {
535538
return err
536539
}
537540
}

client_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func TestBasic(t *testing.T) {
120120
t.Fatalf("AUTH failed: %s", err)
121121
}
122122

123-
if err := c.Rcpt("golang-nuts@googlegroups.com>\r\nDATA\r\nInjected message body\r\n.\r\nQUIT\r\n"); err == nil {
123+
if err := c.Rcpt("golang-nuts@googlegroups.com>\r\nDATA\r\nInjected message body\r\n.\r\nQUIT\r\n", nil); err == nil {
124124
t.Fatalf("RCPT should have failed due to a message injection attempt")
125125
}
126126
if err := c.Mail("user@gmail.com>\r\nDATA\r\nAnother injected message body\r\n.\r\nQUIT\r\n", nil); err == nil {
@@ -129,7 +129,7 @@ func TestBasic(t *testing.T) {
129129
if err := c.Mail("user@gmail.com", nil); err != nil {
130130
t.Fatalf("MAIL failed: %s", err)
131131
}
132-
if err := c.Rcpt("golang-nuts@googlegroups.com"); err != nil {
132+
if err := c.Rcpt("golang-nuts@googlegroups.com", nil); err != nil {
133133
t.Fatalf("RCPT failed: %s", err)
134134
}
135135
msg := `From: user@gmail.com
@@ -797,7 +797,7 @@ func TestLMTP(t *testing.T) {
797797
if err := c.Mail("user@gmail.com", nil); err != nil {
798798
t.Fatalf("MAIL failed: %s", err)
799799
}
800-
if err := c.Rcpt("golang-nuts@googlegroups.com"); err != nil {
800+
if err := c.Rcpt("golang-nuts@googlegroups.com", nil); err != nil {
801801
t.Fatalf("RCPT failed: %s", err)
802802
}
803803
msg := `From: user@gmail.com
@@ -882,10 +882,10 @@ func TestLMTPData(t *testing.T) {
882882
if err := c.Mail("user@gmail.com", nil); err != nil {
883883
t.Fatalf("MAIL failed: %s", err)
884884
}
885-
if err := c.Rcpt("golang-nuts@googlegroups.com"); err != nil {
885+
if err := c.Rcpt("golang-nuts@googlegroups.com", nil); err != nil {
886886
t.Fatalf("RCPT failed: %s", err)
887887
}
888-
if err := c.Rcpt("golang-not-nuts@googlegroups.com"); err != nil {
888+
if err := c.Rcpt("golang-not-nuts@googlegroups.com", nil); err != nil {
889889
t.Fatalf("RCPT failed: %s", err)
890890
}
891891
msg := `From: user@gmail.com

cmd/smtp-debug-server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (s *session) Mail(from string, opts *smtp.MailOptions) error {
3131
return nil
3232
}
3333

34-
func (s *session) Rcpt(to string) error {
34+
func (s *session) Rcpt(to string, opts *smtp.RcptOptions) error {
3535
return nil
3636
}
3737

conn.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,8 @@ func (c *Conn) handleRcpt(arg string) {
473473
return
474474
}
475475

476-
if err := c.Session().Rcpt(recipient); err != nil {
476+
opts := &RcptOptions{}
477+
if err := c.Session().Rcpt(recipient, opts); err != nil {
477478
if smtpErr, ok := err.(*SMTPError); ok {
478479
c.writeResponse(smtpErr.Code, smtpErr.EnhancedCode, smtpErr.Message)
479480
return

example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func ExampleDial() {
2828
if err := c.Mail("sender@example.org", nil); err != nil {
2929
log.Fatal(err)
3030
}
31-
if err := c.Rcpt("recipient@example.net"); err != nil {
31+
if err := c.Rcpt("recipient@example.net", nil); err != nil {
3232
log.Fatal(err)
3333
}
3434

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

116-
func (s *Session) Rcpt(to string) error {
116+
func (s *Session) Rcpt(to string, opts *smtp.RcptOptions) error {
117117
log.Println("Rcpt to:", to)
118118
return nil
119119
}

server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (s *session) Mail(from string, opts *smtp.MailOptions) error {
9494
return nil
9595
}
9696

97-
func (s *session) Rcpt(to string) error {
97+
func (s *session) Rcpt(to string, opts *smtp.RcptOptions) error {
9898
s.msg.To = append(s.msg.To, to)
9999
return nil
100100
}

smtp.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ const (
2424
BodyBinaryMIME BodyType = "BINARYMIME"
2525
)
2626

27-
// MailOptions contains custom arguments that were
28-
// passed as an argument to the MAIL command.
27+
// MailOptions contains parameters for the MAIL command.
2928
type MailOptions struct {
3029
// Value of BODY= argument, 7BIT, 8BITMIME or BINARYMIME.
3130
Body BodyType
@@ -52,3 +51,6 @@ type MailOptions struct {
5251
// Defined in RFC 4954.
5352
Auth *string
5453
}
54+
55+
// RcptOptions contains parameters for the RCPT command.
56+
type RcptOptions struct{}

0 commit comments

Comments
 (0)