Skip to content

Commit

Permalink
Merge pull request #194 from tariq1890/rfc822-email
Browse files Browse the repository at this point in the history
Allows users to submit rfc822 formatted email addresses
  • Loading branch information
thinkingserious authored Oct 23, 2018
2 parents 2438403 + e34dbb7 commit 7440206
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions helpers/mail/mail_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mail
import (
"encoding/json"
"log"
"net/mail"
)

// SGMailV3 contains mail struct
Expand Down Expand Up @@ -674,3 +675,13 @@ func NewSandboxModeSetting(enable bool, forwardSpam bool, spamCheck *SpamCheckSe
SpamCheck: spamCheck,
}
}

// ParseEmail parses a string that contains an rfc822 formatted email address
// and returns an instance of *Email.
func ParseEmail(emailInfo string) (*Email, error) {
e, err := mail.ParseAddress(emailInfo)
if err != nil {
return nil, err
}
return NewEmail(e.Name, e.Address), nil
}
22 changes: 22 additions & 0 deletions helpers/mail/mail_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,3 +726,25 @@ func TestV3NewSandboxModeSetting(t *testing.T) {
assert.True(t, *s.ForwardSpam, "ForwardSpam should be enabled")
assert.NotNil(t, s.SpamCheck, "SpamCheck should not be nil")
}

func TestParseEmail(t *testing.T) {
e, err := ParseEmail("example example <[email protected]>")
if err != nil {
t.Error("Email should have been parsed successfully")
}
expectedName := "example example"
if e.Name != expectedName {
t.Errorf("Expect email with name %s but got %s", expectedName, e.Name)
}
expectedAddress := "[email protected]"
if e.Address != expectedAddress {
t.Errorf("Expect email with address %s but got %s", expectedAddress, e.Address)
}
}

func TestParseInvalidEmail(t *testing.T) {
_, err := ParseEmail("example example <example/example.com>")
if err == nil {
t.Error("Expected an error to be thrown from ParseEmail")
}
}

0 comments on commit 7440206

Please sign in to comment.