Skip to content

Commit

Permalink
client: Change Mail to accept optional MailOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
foxcpp authored and emersion committed Nov 20, 2019
1 parent 959fb82 commit a1945ce
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
25 changes: 7 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,11 @@ func (c *Client) Auth(a sasl.Client) error {
// parameter.
// This initiates a mail transaction and is followed by one or more Rcpt calls.
//
// If server returns an error, it will be of type *SMTPError.
func (c *Client) Mail(from string) error {
return c.MailExt(from, MailOptions{})
}

// MailExt issues a MAIL command to the server using the provided email address.
// If the server supports the 8BITMIME extension, Mail adds the BODY=8BITMIME
// parameter.
// This initiates a mail transaction and is followed by one or more Rcpt calls.
//
// Additionally, MAIL arguments provided in the MailOptions structure
// will be added to the command. Handling of unsupported options
// depends on the extension.
// If opts is not nil, MAIL 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) MailExt(from string, opts MailOptions) error {
func (c *Client) Mail(from string, opts *MailOptions) error {
if err := validateLine(from); err != nil {
return err
}
Expand All @@ -324,17 +313,17 @@ func (c *Client) MailExt(from string, opts MailOptions) error {
if _, ok := c.ext["8BITMIME"]; ok {
cmdStr += " BODY=8BITMIME"
}
if _, ok := c.ext["SIZE"]; ok && opts.Size != 0 {
if _, ok := c.ext["SIZE"]; ok && opts != nil && opts.Size != 0 {
cmdStr += " SIZE=" + strconv.Itoa(opts.Size)
}
if opts.RequireTLS {
if opts != nil && opts.RequireTLS {
if _, ok := c.ext["REQUIRETLS"]; ok {
cmdStr += " REQUIRETLS"
} else {
return errors.New("smtp: server does not support REQUIRETLS")
}
}
if opts.UTF8 {
if opts != nil && opts.UTF8 {
if _, ok := c.ext["SMTPUTF8"]; ok {
cmdStr += " SMTPUTF8"
} else {
Expand Down Expand Up @@ -458,7 +447,7 @@ func SendMail(addr string, a sasl.Client, from string, to []string, r io.Reader)
return err
}
}
if err = c.Mail(from); err != nil {
if err = c.Mail(from, nil); err != nil {
return err
}
for _, addr := range to {
Expand Down
14 changes: 7 additions & 7 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestBasic(t *testing.T) {
t.Fatalf("Shouldn't support DSN")
}

if err := c.Mail("[email protected]"); err == nil {
if err := c.Mail("[email protected]", nil); err == nil {
t.Fatalf("MAIL should require authentication")
}

Expand All @@ -123,10 +123,10 @@ func TestBasic(t *testing.T) {
if err := c.Rcpt("[email protected]>\r\nDATA\r\nInjected message body\r\n.\r\nQUIT\r\n"); 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"); err == nil {
if err := c.Mail("[email protected]>\r\nDATA\r\nAnother injected message body\r\n.\r\nQUIT\r\n", nil); err == nil {
t.Fatalf("MAIL should have failed due to a message injection attempt")
}
if err := c.Mail("[email protected]"); err != nil {
if err := c.Mail("[email protected]", nil); err != nil {
t.Fatalf("MAIL failed: %s", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestBasic_SMTPError(t *testing.T) {
t.Fatalf("NewClient failed: %v", err)
}

err = c.Mail("whatever")
err = c.Mail("whatever", nil)
if err == nil {
t.Fatal("MAIL succeded")
}
Expand All @@ -205,7 +205,7 @@ func TestBasic_SMTPError(t *testing.T) {
t.Fatalf("Wrong message, got %s, want %s", smtpErr.Message, "Failing with enhanced code")
}

err = c.Mail("whatever")
err = c.Mail("whatever", nil)
if err == nil {
t.Fatal("MAIL succeded")
}
Expand Down Expand Up @@ -385,7 +385,7 @@ func TestHello(t *testing.T) {
c.serverName = "smtp.google.com"
err = c.Auth(sasl.NewPlainClient("", "user", "pass"))
case 4:
err = c.Mail("[email protected]")
err = c.Mail("[email protected]", nil)
case 5:
ok, _ := c.Extension("feature")
if ok {
Expand Down Expand Up @@ -851,7 +851,7 @@ func TestLMTP(t *testing.T) {
}
c.didHello = true

if err := c.Mail("[email protected]"); err != nil {
if err := c.Mail("[email protected]", nil); err != nil {
t.Fatalf("MAIL failed: %s", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func ExampleDial() {
}

// Set the sender and recipient first
if err := c.Mail("[email protected]"); err != nil {
if err := c.Mail("[email protected]", nil); err != nil {
log.Fatal(err)
}
if err := c.Rcpt("[email protected]"); err != nil {
Expand Down

0 comments on commit a1945ce

Please sign in to comment.