Skip to content

Commit

Permalink
Rfc1870 fix (#25)
Browse files Browse the repository at this point in the history
* Add SIZE extension for mail from as it described in RFC 1870

Co-authored-by: Шипицын Анатолий <[email protected]>
  • Loading branch information
norguhtar and Шипицын Анатолий authored Feb 15, 2021
1 parent 8e470cb commit d423a91
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
10 changes: 9 additions & 1 deletion email.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/mail"
"net/textproto"
"path/filepath"
"strconv"
"time"
)

Expand Down Expand Up @@ -970,8 +971,15 @@ func send(from string, to []string, msg string, client *SMTPClient) error {
}

func sendMailProcess(from string, to []string, msg string, c *smtpClient) error {

cmdArgs := make(map[string]string)

if _, ok := c.ext["SIZE"]; ok {
cmdArgs["SIZE"] = strconv.Itoa(len(msg))
}

// Set the sender
if err := c.mail(from); err != nil {
if err := c.mail(from, cmdArgs); err != nil {
return err
}

Expand Down
19 changes: 17 additions & 2 deletions smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// SMTPUTF8 RFC 6531
// AUTH RFC 2554
// STARTTLS RFC 3207
// SIZE RFC 1870
// Additional extensions may be handled by clients using smtp.go in golang source code or pull request Go Simple Mail

// smtp.go file is a modification of smtp golang package what is frozen and is not accepting new features.
Expand Down Expand Up @@ -206,7 +207,14 @@ func (c *smtpClient) authenticate(a auth) error {
// If the server supports the SMTPUTF8 extension, Mail adds the
// SMTPUTF8 parameter.
// This initiates a mail transaction and is followed by one or more Rcpt calls.
func (c *smtpClient) mail(from string) error {
func (c *smtpClient) mail(from string, extArgs ...map[string]string) error {
var args []interface{}
var extMap map[string]string

if len(extArgs) > 0 {
extMap = extArgs[0]
}

if err := validateLine(from); err != nil {
return err
}
Expand All @@ -221,8 +229,15 @@ func (c *smtpClient) mail(from string) error {
if _, ok := c.ext["SMTPUTF8"]; ok {
cmdStr += " SMTPUTF8"
}
if _, ok := c.ext["SIZE"]; ok {
if extMap["SIZE"] != "" {
cmdStr += " SIZE=%s"
args = append(args, extMap["SIZE"])
}
}
}
_, _, err := c.cmd(250, cmdStr, from)
args = append([]interface{}{from}, args...)
_, _, err := c.cmd(250, cmdStr, args...)
return err
}

Expand Down
46 changes: 46 additions & 0 deletions smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,52 @@ QUIT
}
})

t.Run("ehlo size", func(t *testing.T) {
const (
basicServer = `250-mx.google.com at your service
250-SIZE 35651584
250 [email protected] OK; can accommodate 500000 byte message
250 Sender OK
221 Goodbye
`

basicClient = `EHLO localhost
MAIL FROM:<[email protected]> SIZE=50000
QUIT
`
)

c, bcmdbuf, cmdbuf := faker(basicServer)
cmdArgs := make(map[string]string)

if err := c.hi("localhost"); err != nil {
t.Fatalf("EHLO failed: %s", err)
}
if ok, _ := c.extension("SIZE"); !ok {
t.Fatalf("Should support SIZE")
}
if ok, _ := c.extension("SMTPUTF8"); ok {
t.Fatalf("Shouldn't support SMTPUTF8")
}

if ok, _ := c.extension("SIZE"); ok {
cmdArgs["SIZE"] = "50000"
}
if err := c.mail("[email protected]", cmdArgs); err != nil {
t.Fatalf("MAIL FROM failed: %s", err)
}
if err := c.quit(); err != nil {
t.Fatalf("QUIT failed: %s", err)
}

bcmdbuf.Flush()
actualcmds := cmdbuf.String()
client := strings.Join(strings.Split(basicClient, "\n"), "\r\n")
if client != actualcmds {
t.Fatalf("Got:\n%s\nExpected:\n%s", actualcmds, client)
}
})

t.Run("ehlo 8bitmime", func(t *testing.T) {
const (
basicServer = `250-mx.google.com at your service
Expand Down

0 comments on commit d423a91

Please sign in to comment.